blob: 8003c56487bb8ebd6c3d3e5424b5a38a1babaaeb [file] [log] [blame]
Chaithrika U Se7332e32009-06-09 05:55:37 -03001/*
2 * vpif-display - VPIF display driver
3 * Display driver for TI DaVinci VPIF
4 *
5 * Copyright (C) 2009 Texas Instruments Incorporated - http://www.ti.com/
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation version 2.
10 *
11 * This program is distributed .as is. WITHOUT ANY WARRANTY of any
12 * kind, whether express or implied; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 */
16
17#include <linux/kernel.h>
18#include <linux/init.h>
19#include <linux/module.h>
20#include <linux/errno.h>
21#include <linux/fs.h>
22#include <linux/mm.h>
23#include <linux/interrupt.h>
24#include <linux/workqueue.h>
25#include <linux/string.h>
26#include <linux/videodev2.h>
27#include <linux/wait.h>
28#include <linux/time.h>
29#include <linux/i2c.h>
30#include <linux/platform_device.h>
31#include <linux/io.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090032#include <linux/slab.h>
Chaithrika U Se7332e32009-06-09 05:55:37 -030033
34#include <asm/irq.h>
35#include <asm/page.h>
36
37#include <media/adv7343.h>
38#include <media/v4l2-device.h>
39#include <media/v4l2-ioctl.h>
Mats Randgaard7036d6a2010-12-16 12:17:41 -030040#include <media/v4l2-chip-ident.h>
Chaithrika U Se7332e32009-06-09 05:55:37 -030041
Chaithrika U Se7332e32009-06-09 05:55:37 -030042#include "vpif_display.h"
43#include "vpif.h"
44
45MODULE_DESCRIPTION("TI DaVinci VPIF Display driver");
46MODULE_LICENSE("GPL");
Mauro Carvalho Chehab64dc3c12011-06-25 11:28:37 -030047MODULE_VERSION(VPIF_DISPLAY_VERSION);
Chaithrika U Se7332e32009-06-09 05:55:37 -030048
Manjunath Hadli0a631722012-04-13 04:44:00 -030049#define VPIF_V4L2_STD (V4L2_STD_525_60 | V4L2_STD_625_50)
Chaithrika U Se7332e32009-06-09 05:55:37 -030050
51#define vpif_err(fmt, arg...) v4l2_err(&vpif_obj.v4l2_dev, fmt, ## arg)
52#define vpif_dbg(level, debug, fmt, arg...) \
53 v4l2_dbg(level, debug, &vpif_obj.v4l2_dev, fmt, ## arg)
54
55static int debug = 1;
56static u32 ch2_numbuffers = 3;
57static u32 ch3_numbuffers = 3;
58static u32 ch2_bufsize = 1920 * 1080 * 2;
59static u32 ch3_bufsize = 720 * 576 * 2;
60
61module_param(debug, int, 0644);
62module_param(ch2_numbuffers, uint, S_IRUGO);
63module_param(ch3_numbuffers, uint, S_IRUGO);
64module_param(ch2_bufsize, uint, S_IRUGO);
65module_param(ch3_bufsize, uint, S_IRUGO);
66
67MODULE_PARM_DESC(debug, "Debug level 0-1");
68MODULE_PARM_DESC(ch2_numbuffers, "Channel2 buffer count (default:3)");
69MODULE_PARM_DESC(ch3_numbuffers, "Channel3 buffer count (default:3)");
70MODULE_PARM_DESC(ch2_bufsize, "Channel2 buffer size (default:1920 x 1080 x 2)");
71MODULE_PARM_DESC(ch3_bufsize, "Channel3 buffer size (default:720 x 576 x 2)");
72
73static struct vpif_config_params config_params = {
74 .min_numbuffers = 3,
75 .numbuffers[0] = 3,
76 .numbuffers[1] = 3,
77 .min_bufsize[0] = 720 * 480 * 2,
78 .min_bufsize[1] = 720 * 480 * 2,
79 .channel_bufsize[0] = 1920 * 1080 * 2,
80 .channel_bufsize[1] = 720 * 576 * 2,
81};
82
83static struct vpif_device vpif_obj = { {NULL} };
84static struct device *vpif_dev;
Lad, Prabhakar2401dd22012-06-28 09:28:36 -030085static void vpif_calculate_offsets(struct channel_obj *ch);
86static void vpif_config_addr(struct channel_obj *ch, int muxmode);
Chaithrika U Se7332e32009-06-09 05:55:37 -030087
Chaithrika U Se7332e32009-06-09 05:55:37 -030088/*
Lad, Prabhakar2401dd22012-06-28 09:28:36 -030089 * buffer_prepare: This is the callback function called from vb2_qbuf()
Chaithrika U Se7332e32009-06-09 05:55:37 -030090 * function the buffer is prepared and user space virtual address is converted
91 * into physical address
92 */
Lad, Prabhakar2401dd22012-06-28 09:28:36 -030093static int vpif_buffer_prepare(struct vb2_buffer *vb)
Chaithrika U Se7332e32009-06-09 05:55:37 -030094{
Lad, Prabhakar2401dd22012-06-28 09:28:36 -030095 struct vpif_fh *fh = vb2_get_drv_priv(vb->vb2_queue);
96 struct vb2_queue *q = vb->vb2_queue;
Chaithrika U Se7332e32009-06-09 05:55:37 -030097 struct common_obj *common;
98 unsigned long addr;
99
100 common = &fh->channel->common[VPIF_VIDEO_INDEX];
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300101 if (vb->state != VB2_BUF_STATE_ACTIVE &&
102 vb->state != VB2_BUF_STATE_PREPARED) {
103 vb2_set_plane_payload(vb, 0, common->fmt.fmt.pix.sizeimage);
104 if (vb2_plane_vaddr(vb, 0) &&
105 vb2_get_plane_payload(vb, 0) > vb2_plane_size(vb, 0))
106 goto buf_align_exit;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300107
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300108 addr = vb2_dma_contig_plane_dma_addr(vb, 0);
109 if (q->streaming &&
110 (V4L2_BUF_TYPE_SLICED_VBI_OUTPUT != q->type)) {
111 if (!ISALIGNED(addr + common->ytop_off) ||
112 !ISALIGNED(addr + common->ybtm_off) ||
113 !ISALIGNED(addr + common->ctop_off) ||
114 !ISALIGNED(addr + common->cbtm_off))
115 goto buf_align_exit;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300116 }
Chaithrika U Se7332e32009-06-09 05:55:37 -0300117 }
118 return 0;
119
120buf_align_exit:
121 vpif_err("buffer offset not aligned to 8 bytes\n");
122 return -EINVAL;
123}
124
125/*
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300126 * vpif_buffer_queue_setup: This function allocates memory for the buffers
Chaithrika U Se7332e32009-06-09 05:55:37 -0300127 */
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300128static int vpif_buffer_queue_setup(struct vb2_queue *vq,
129 const struct v4l2_format *fmt,
130 unsigned int *nbuffers, unsigned int *nplanes,
131 unsigned int sizes[], void *alloc_ctxs[])
Chaithrika U Se7332e32009-06-09 05:55:37 -0300132{
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300133 struct vpif_fh *fh = vb2_get_drv_priv(vq);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300134 struct channel_obj *ch = fh->channel;
135 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300136 unsigned long size;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300137
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300138 if (V4L2_MEMORY_MMAP == common->memory) {
139 size = config_params.channel_bufsize[ch->channel_id];
140 /*
141 * Checking if the buffer size exceeds the available buffer
142 * ycmux_mode = 0 means 1 channel mode HD and
143 * ycmux_mode = 1 means 2 channels mode SD
144 */
145 if (ch->vpifparams.std_info.ycmux_mode == 0) {
146 if (config_params.video_limit[ch->channel_id])
147 while (size * *nbuffers >
148 (config_params.video_limit[0]
149 + config_params.video_limit[1]))
150 (*nbuffers)--;
151 } else {
152 if (config_params.video_limit[ch->channel_id])
153 while (size * *nbuffers >
Manjunath Hadlifc613d42012-04-13 04:49:10 -0300154 config_params.video_limit[ch->channel_id])
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300155 (*nbuffers)--;
156 }
157 } else {
158 size = common->fmt.fmt.pix.sizeimage;
Manjunath Hadlifc613d42012-04-13 04:49:10 -0300159 }
160
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300161 if (*nbuffers < config_params.min_numbuffers)
162 *nbuffers = config_params.min_numbuffers;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300163
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300164 *nplanes = 1;
165 sizes[0] = size;
166 alloc_ctxs[0] = common->alloc_ctx;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300167 return 0;
168}
169
170/*
171 * vpif_buffer_queue: This function adds the buffer to DMA queue
172 */
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300173static void vpif_buffer_queue(struct vb2_buffer *vb)
Chaithrika U Se7332e32009-06-09 05:55:37 -0300174{
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300175 struct vpif_fh *fh = vb2_get_drv_priv(vb->vb2_queue);
176 struct vpif_disp_buffer *buf = container_of(vb,
177 struct vpif_disp_buffer, vb);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300178 struct channel_obj *ch = fh->channel;
179 struct common_obj *common;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300180
181 common = &ch->common[VPIF_VIDEO_INDEX];
182
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300183 /* add the buffer to the DMA queue */
184 list_add_tail(&buf->list, &common->dma_queue);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300185}
186
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300187/*
188 * vpif_buf_cleanup: This function is called from the videobuf2 layer to
189 * free memory allocated to the buffers
190 */
191static void vpif_buf_cleanup(struct vb2_buffer *vb)
192{
193 struct vpif_fh *fh = vb2_get_drv_priv(vb->vb2_queue);
194 struct vpif_disp_buffer *buf = container_of(vb,
195 struct vpif_disp_buffer, vb);
196 struct channel_obj *ch = fh->channel;
197 struct common_obj *common;
198 unsigned long flags;
199
200 common = &ch->common[VPIF_VIDEO_INDEX];
201
202 spin_lock_irqsave(&common->irqlock, flags);
203 if (vb->state == VB2_BUF_STATE_ACTIVE)
204 list_del_init(&buf->list);
205 spin_unlock_irqrestore(&common->irqlock, flags);
206}
207
208static void vpif_wait_prepare(struct vb2_queue *vq)
209{
210 struct vpif_fh *fh = vb2_get_drv_priv(vq);
211 struct channel_obj *ch = fh->channel;
212 struct common_obj *common;
213
214 common = &ch->common[VPIF_VIDEO_INDEX];
215 mutex_unlock(&common->lock);
216}
217
218static void vpif_wait_finish(struct vb2_queue *vq)
219{
220 struct vpif_fh *fh = vb2_get_drv_priv(vq);
221 struct channel_obj *ch = fh->channel;
222 struct common_obj *common;
223
224 common = &ch->common[VPIF_VIDEO_INDEX];
225 mutex_lock(&common->lock);
226}
227
228static int vpif_buffer_init(struct vb2_buffer *vb)
229{
230 struct vpif_disp_buffer *buf = container_of(vb,
231 struct vpif_disp_buffer, vb);
232
233 INIT_LIST_HEAD(&buf->list);
234
235 return 0;
236}
237
Chaithrika U Se7332e32009-06-09 05:55:37 -0300238static u8 channel_first_int[VPIF_NUMOBJECTS][2] = { {1, 1} };
239
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300240static int vpif_start_streaming(struct vb2_queue *vq, unsigned int count)
241{
242 struct vpif_display_config *vpif_config_data =
243 vpif_dev->platform_data;
244 struct vpif_fh *fh = vb2_get_drv_priv(vq);
245 struct channel_obj *ch = fh->channel;
246 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
247 struct vpif_params *vpif = &ch->vpifparams;
248 unsigned long addr = 0;
249 int ret;
250
251 /* If buffer queue is empty, return error */
252 if (list_empty(&common->dma_queue)) {
253 vpif_err("buffer queue is empty\n");
254 return -EIO;
255 }
256
257 /* Get the next frame from the buffer queue */
258 common->next_frm = common->cur_frm =
259 list_entry(common->dma_queue.next,
260 struct vpif_disp_buffer, list);
261
262 list_del(&common->cur_frm->list);
263 /* Mark state of the current frame to active */
264 common->cur_frm->vb.state = VB2_BUF_STATE_ACTIVE;
265
266 /* Initialize field_id and started member */
267 ch->field_id = 0;
268 common->started = 1;
269 addr = vb2_dma_contig_plane_dma_addr(&common->cur_frm->vb, 0);
270 /* Calculate the offset for Y and C data in the buffer */
271 vpif_calculate_offsets(ch);
272
273 if ((ch->vpifparams.std_info.frm_fmt &&
274 ((common->fmt.fmt.pix.field != V4L2_FIELD_NONE)
275 && (common->fmt.fmt.pix.field != V4L2_FIELD_ANY)))
276 || (!ch->vpifparams.std_info.frm_fmt
277 && (common->fmt.fmt.pix.field == V4L2_FIELD_NONE))) {
278 vpif_err("conflict in field format and std format\n");
279 return -EINVAL;
280 }
281
282 /* clock settings */
283 ret =
284 vpif_config_data->set_clock(ch->vpifparams.std_info.ycmux_mode,
285 ch->vpifparams.std_info.hd_sd);
286 if (ret < 0) {
287 vpif_err("can't set clock\n");
288 return ret;
289 }
290
291 /* set the parameters and addresses */
292 ret = vpif_set_video_params(vpif, ch->channel_id + 2);
293 if (ret < 0)
294 return ret;
295
296 common->started = ret;
297 vpif_config_addr(ch, ret);
298 common->set_addr((addr + common->ytop_off),
299 (addr + common->ybtm_off),
300 (addr + common->ctop_off),
301 (addr + common->cbtm_off));
302
303 /* Set interrupt for both the fields in VPIF
304 Register enable channel in VPIF register */
305 if (VPIF_CHANNEL2_VIDEO == ch->channel_id) {
306 channel2_intr_assert();
307 channel2_intr_enable(1);
308 enable_channel2(1);
Manjunath Hadli6964b102012-06-29 03:20:12 -0300309 if (vpif_config_data->ch2_clip_en)
310 channel2_clipping_enable(1);
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300311 }
312
313 if ((VPIF_CHANNEL3_VIDEO == ch->channel_id)
314 || (common->started == 2)) {
315 channel3_intr_assert();
316 channel3_intr_enable(1);
317 enable_channel3(1);
Manjunath Hadli6964b102012-06-29 03:20:12 -0300318 if (vpif_config_data->ch3_clip_en)
319 channel3_clipping_enable(1);
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300320 }
321 channel_first_int[VPIF_VIDEO_INDEX][ch->channel_id] = 1;
322
323 return 0;
324}
325
326/* abort streaming and wait for last buffer */
327static int vpif_stop_streaming(struct vb2_queue *vq)
328{
329 struct vpif_fh *fh = vb2_get_drv_priv(vq);
330 struct channel_obj *ch = fh->channel;
331 struct common_obj *common;
332
333 if (!vb2_is_streaming(vq))
334 return 0;
335
336 common = &ch->common[VPIF_VIDEO_INDEX];
337
338 /* release all active buffers */
339 while (!list_empty(&common->dma_queue)) {
340 common->next_frm = list_entry(common->dma_queue.next,
341 struct vpif_disp_buffer, list);
342 list_del(&common->next_frm->list);
343 vb2_buffer_done(&common->next_frm->vb, VB2_BUF_STATE_ERROR);
344 }
345
346 return 0;
347}
348
349static struct vb2_ops video_qops = {
350 .queue_setup = vpif_buffer_queue_setup,
351 .wait_prepare = vpif_wait_prepare,
352 .wait_finish = vpif_wait_finish,
353 .buf_init = vpif_buffer_init,
354 .buf_prepare = vpif_buffer_prepare,
355 .start_streaming = vpif_start_streaming,
356 .stop_streaming = vpif_stop_streaming,
357 .buf_cleanup = vpif_buf_cleanup,
358 .buf_queue = vpif_buffer_queue,
359};
360
Chaithrika U Se7332e32009-06-09 05:55:37 -0300361static void process_progressive_mode(struct common_obj *common)
362{
363 unsigned long addr = 0;
364
365 /* Get the next buffer from buffer queue */
366 common->next_frm = list_entry(common->dma_queue.next,
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300367 struct vpif_disp_buffer, list);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300368 /* Remove that buffer from the buffer queue */
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300369 list_del(&common->next_frm->list);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300370 /* Mark status of the buffer as active */
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300371 common->next_frm->vb.state = VB2_BUF_STATE_ACTIVE;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300372
373 /* Set top and bottom field addrs in VPIF registers */
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300374 addr = vb2_dma_contig_plane_dma_addr(&common->next_frm->vb, 0);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300375 common->set_addr(addr + common->ytop_off,
376 addr + common->ybtm_off,
377 addr + common->ctop_off,
378 addr + common->cbtm_off);
379}
380
381static void process_interlaced_mode(int fid, struct common_obj *common)
382{
383 /* device field id and local field id are in sync */
384 /* If this is even field */
385 if (0 == fid) {
386 if (common->cur_frm == common->next_frm)
387 return;
388
389 /* one frame is displayed If next frame is
390 * available, release cur_frm and move on */
391 /* Copy frame display time */
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300392 do_gettimeofday(&common->cur_frm->vb.v4l2_buf.timestamp);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300393 /* Change status of the cur_frm */
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300394 vb2_buffer_done(&common->cur_frm->vb,
395 VB2_BUF_STATE_DONE);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300396 /* Make cur_frm pointing to next_frm */
397 common->cur_frm = common->next_frm;
398
399 } else if (1 == fid) { /* odd field */
400 if (list_empty(&common->dma_queue)
401 || (common->cur_frm != common->next_frm)) {
402 return;
403 }
404 /* one field is displayed configure the next
405 * frame if it is available else hold on current
406 * frame */
407 /* Get next from the buffer queue */
408 process_progressive_mode(common);
409
410 }
411}
412
413/*
414 * vpif_channel_isr: It changes status of the displayed buffer, takes next
415 * buffer from the queue and sets its address in VPIF registers
416 */
417static irqreturn_t vpif_channel_isr(int irq, void *dev_id)
418{
419 struct vpif_device *dev = &vpif_obj;
420 struct channel_obj *ch;
421 struct common_obj *common;
422 enum v4l2_field field;
423 int fid = -1, i;
424 int channel_id = 0;
425
426 channel_id = *(int *)(dev_id);
Manjunath Hadlib1fc4232012-04-13 04:43:10 -0300427 if (!vpif_intr_status(channel_id + 2))
428 return IRQ_NONE;
429
Chaithrika U Se7332e32009-06-09 05:55:37 -0300430 ch = dev->dev[channel_id];
431 field = ch->common[VPIF_VIDEO_INDEX].fmt.fmt.pix.field;
432 for (i = 0; i < VPIF_NUMOBJECTS; i++) {
433 common = &ch->common[i];
434 /* If streaming is started in this channel */
435 if (0 == common->started)
436 continue;
437
438 if (1 == ch->vpifparams.std_info.frm_fmt) {
439 if (list_empty(&common->dma_queue))
440 continue;
441
442 /* Progressive mode */
443 if (!channel_first_int[i][channel_id]) {
444 /* Mark status of the cur_frm to
445 * done and unlock semaphore on it */
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300446 do_gettimeofday(&common->cur_frm->vb.
447 v4l2_buf.timestamp);
448 vb2_buffer_done(&common->cur_frm->vb,
449 VB2_BUF_STATE_DONE);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300450 /* Make cur_frm pointing to next_frm */
451 common->cur_frm = common->next_frm;
452 }
453
454 channel_first_int[i][channel_id] = 0;
455 process_progressive_mode(common);
456 } else {
457 /* Interlaced mode */
458 /* If it is first interrupt, ignore it */
459
460 if (channel_first_int[i][channel_id]) {
461 channel_first_int[i][channel_id] = 0;
462 continue;
463 }
464
465 if (0 == i) {
466 ch->field_id ^= 1;
467 /* Get field id from VPIF registers */
468 fid = vpif_channel_getfid(ch->channel_id + 2);
469 /* If fid does not match with stored field id */
470 if (fid != ch->field_id) {
471 /* Make them in sync */
472 if (0 == fid)
473 ch->field_id = fid;
474
475 return IRQ_HANDLED;
476 }
477 }
478 process_interlaced_mode(fid, common);
479 }
480 }
481
482 return IRQ_HANDLED;
483}
484
Mats Randgaardc027e162010-12-16 12:17:44 -0300485static int vpif_update_std_info(struct channel_obj *ch)
Chaithrika U Se7332e32009-06-09 05:55:37 -0300486{
Chaithrika U Se7332e32009-06-09 05:55:37 -0300487 struct video_obj *vid_ch = &ch->video;
488 struct vpif_params *vpifparams = &ch->vpifparams;
489 struct vpif_channel_config_params *std_info = &vpifparams->std_info;
490 const struct vpif_channel_config_params *config;
491
Mats Randgaardc027e162010-12-16 12:17:44 -0300492 int i;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300493
Mats Randgaardc027e162010-12-16 12:17:44 -0300494 for (i = 0; i < vpif_ch_params_count; i++) {
495 config = &ch_params[i];
Mats Randgaard40c8bce2010-12-16 12:17:43 -0300496 if (config->hd_sd == 0) {
497 vpif_dbg(2, debug, "SD format\n");
498 if (config->stdid & vid_ch->stdid) {
499 memcpy(std_info, config, sizeof(*config));
500 break;
501 }
Chaithrika U Se7332e32009-06-09 05:55:37 -0300502 }
503 }
504
Mats Randgaardc027e162010-12-16 12:17:44 -0300505 if (i == vpif_ch_params_count) {
506 vpif_dbg(1, debug, "Format not found\n");
Mats Randgaardaa444402010-12-16 12:17:42 -0300507 return -EINVAL;
Mats Randgaardc027e162010-12-16 12:17:44 -0300508 }
509
510 return 0;
511}
512
513static int vpif_update_resolution(struct channel_obj *ch)
514{
515 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
516 struct video_obj *vid_ch = &ch->video;
517 struct vpif_params *vpifparams = &ch->vpifparams;
518 struct vpif_channel_config_params *std_info = &vpifparams->std_info;
519
Hans Verkuil0598c172012-09-18 07:18:47 -0300520 if (!vid_ch->stdid && !vid_ch->dv_timings.bt.height)
Mats Randgaardc027e162010-12-16 12:17:44 -0300521 return -EINVAL;
522
Hans Verkuil0598c172012-09-18 07:18:47 -0300523 if (vid_ch->stdid) {
Mats Randgaardc027e162010-12-16 12:17:44 -0300524 if (vpif_update_std_info(ch))
525 return -EINVAL;
526 }
Chaithrika U Se7332e32009-06-09 05:55:37 -0300527
528 common->fmt.fmt.pix.width = std_info->width;
529 common->fmt.fmt.pix.height = std_info->height;
530 vpif_dbg(1, debug, "Pixel details: Width = %d,Height = %d\n",
531 common->fmt.fmt.pix.width, common->fmt.fmt.pix.height);
532
533 /* Set height and width paramateres */
Mats Randgaardc027e162010-12-16 12:17:44 -0300534 common->height = std_info->height;
535 common->width = std_info->width;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300536
537 return 0;
538}
539
540/*
541 * vpif_calculate_offsets: This function calculates buffers offset for Y and C
542 * in the top and bottom field
543 */
544static void vpif_calculate_offsets(struct channel_obj *ch)
545{
546 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
547 struct vpif_params *vpifparams = &ch->vpifparams;
548 enum v4l2_field field = common->fmt.fmt.pix.field;
549 struct video_obj *vid_ch = &ch->video;
550 unsigned int hpitch, vpitch, sizeimage;
551
552 if (V4L2_FIELD_ANY == common->fmt.fmt.pix.field) {
553 if (ch->vpifparams.std_info.frm_fmt)
554 vid_ch->buf_field = V4L2_FIELD_NONE;
555 else
556 vid_ch->buf_field = V4L2_FIELD_INTERLACED;
557 } else {
558 vid_ch->buf_field = common->fmt.fmt.pix.field;
559 }
560
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300561 sizeimage = common->fmt.fmt.pix.sizeimage;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300562
563 hpitch = common->fmt.fmt.pix.bytesperline;
564 vpitch = sizeimage / (hpitch * 2);
565 if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
566 (V4L2_FIELD_INTERLACED == vid_ch->buf_field)) {
567 common->ytop_off = 0;
568 common->ybtm_off = hpitch;
569 common->ctop_off = sizeimage / 2;
570 common->cbtm_off = sizeimage / 2 + hpitch;
571 } else if (V4L2_FIELD_SEQ_TB == vid_ch->buf_field) {
572 common->ytop_off = 0;
573 common->ybtm_off = sizeimage / 4;
574 common->ctop_off = sizeimage / 2;
575 common->cbtm_off = common->ctop_off + sizeimage / 4;
576 } else if (V4L2_FIELD_SEQ_BT == vid_ch->buf_field) {
577 common->ybtm_off = 0;
578 common->ytop_off = sizeimage / 4;
579 common->cbtm_off = sizeimage / 2;
580 common->ctop_off = common->cbtm_off + sizeimage / 4;
581 }
582
583 if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
584 (V4L2_FIELD_INTERLACED == vid_ch->buf_field)) {
585 vpifparams->video_params.storage_mode = 1;
586 } else {
587 vpifparams->video_params.storage_mode = 0;
588 }
589
590 if (ch->vpifparams.std_info.frm_fmt == 1) {
591 vpifparams->video_params.hpitch =
592 common->fmt.fmt.pix.bytesperline;
593 } else {
594 if ((field == V4L2_FIELD_ANY) ||
595 (field == V4L2_FIELD_INTERLACED))
596 vpifparams->video_params.hpitch =
597 common->fmt.fmt.pix.bytesperline * 2;
598 else
599 vpifparams->video_params.hpitch =
600 common->fmt.fmt.pix.bytesperline;
601 }
602
603 ch->vpifparams.video_params.stdid = ch->vpifparams.std_info.stdid;
604}
605
606static void vpif_config_format(struct channel_obj *ch)
607{
608 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
609
610 common->fmt.fmt.pix.field = V4L2_FIELD_ANY;
611 if (config_params.numbuffers[ch->channel_id] == 0)
612 common->memory = V4L2_MEMORY_USERPTR;
613 else
614 common->memory = V4L2_MEMORY_MMAP;
615
616 common->fmt.fmt.pix.sizeimage =
617 config_params.channel_bufsize[ch->channel_id];
618 common->fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUV422P;
619 common->fmt.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
620}
621
622static int vpif_check_format(struct channel_obj *ch,
623 struct v4l2_pix_format *pixfmt)
624{
625 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
626 enum v4l2_field field = pixfmt->field;
627 u32 sizeimage, hpitch, vpitch;
628
629 if (pixfmt->pixelformat != V4L2_PIX_FMT_YUV422P)
630 goto invalid_fmt_exit;
631
632 if (!(VPIF_VALID_FIELD(field)))
633 goto invalid_fmt_exit;
634
635 if (pixfmt->bytesperline <= 0)
636 goto invalid_pitch_exit;
637
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300638 sizeimage = pixfmt->sizeimage;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300639
Mats Randgaardc027e162010-12-16 12:17:44 -0300640 if (vpif_update_resolution(ch))
Chaithrika U Se7332e32009-06-09 05:55:37 -0300641 return -EINVAL;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300642
643 hpitch = pixfmt->bytesperline;
644 vpitch = sizeimage / (hpitch * 2);
645
646 /* Check for valid value of pitch */
647 if ((hpitch < ch->vpifparams.std_info.width) ||
648 (vpitch < ch->vpifparams.std_info.height))
649 goto invalid_pitch_exit;
650
651 /* Check for 8 byte alignment */
652 if (!ISALIGNED(hpitch)) {
653 vpif_err("invalid pitch alignment\n");
654 return -EINVAL;
655 }
656 pixfmt->width = common->fmt.fmt.pix.width;
657 pixfmt->height = common->fmt.fmt.pix.height;
658
659 return 0;
660
661invalid_fmt_exit:
662 vpif_err("invalid field format\n");
663 return -EINVAL;
664
665invalid_pitch_exit:
666 vpif_err("invalid pitch\n");
667 return -EINVAL;
668}
669
670static void vpif_config_addr(struct channel_obj *ch, int muxmode)
671{
672 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
673
674 if (VPIF_CHANNEL3_VIDEO == ch->channel_id) {
675 common->set_addr = ch3_set_videobuf_addr;
676 } else {
677 if (2 == muxmode)
678 common->set_addr = ch2_set_videobuf_addr_yc_nmux;
679 else
680 common->set_addr = ch2_set_videobuf_addr;
681 }
682}
683
684/*
685 * vpif_mmap: It is used to map kernel space buffers into user spaces
686 */
687static int vpif_mmap(struct file *filep, struct vm_area_struct *vma)
688{
689 struct vpif_fh *fh = filep->private_data;
Mats Randgaard2c0ddd12010-12-16 12:17:45 -0300690 struct channel_obj *ch = fh->channel;
691 struct common_obj *common = &(ch->common[VPIF_VIDEO_INDEX]);
Hans Verkuil0b7286d2012-07-31 03:47:07 -0300692 int ret;
Mats Randgaard2c0ddd12010-12-16 12:17:45 -0300693
694 vpif_dbg(2, debug, "vpif_mmap\n");
Chaithrika U Se7332e32009-06-09 05:55:37 -0300695
Hans Verkuil0b7286d2012-07-31 03:47:07 -0300696 if (mutex_lock_interruptible(&common->lock))
697 return -ERESTARTSYS;
698 ret = vb2_mmap(&common->buffer_queue, vma);
699 mutex_unlock(&common->lock);
700 return ret;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300701}
702
703/*
704 * vpif_poll: It is used for select/poll system call
705 */
706static unsigned int vpif_poll(struct file *filep, poll_table *wait)
707{
708 struct vpif_fh *fh = filep->private_data;
709 struct channel_obj *ch = fh->channel;
710 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
Hans Verkuil0b7286d2012-07-31 03:47:07 -0300711 unsigned int res = 0;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300712
Hans Verkuil0b7286d2012-07-31 03:47:07 -0300713 if (common->started) {
714 mutex_lock(&common->lock);
715 res = vb2_poll(&common->buffer_queue, filep, wait);
716 mutex_unlock(&common->lock);
717 }
Chaithrika U Se7332e32009-06-09 05:55:37 -0300718
Hans Verkuil0b7286d2012-07-31 03:47:07 -0300719 return res;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300720}
721
722/*
723 * vpif_open: It creates object of file handle structure and stores it in
724 * private_data member of filepointer
725 */
726static int vpif_open(struct file *filep)
727{
728 struct video_device *vdev = video_devdata(filep);
Hans Verkuil0b7286d2012-07-31 03:47:07 -0300729 struct channel_obj *ch = video_get_drvdata(vdev);
730 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
731 struct vpif_fh *fh;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300732
Chaithrika U Se7332e32009-06-09 05:55:37 -0300733 /* Allocate memory for the file handle object */
Mats Randgaard1f8766b2010-08-30 10:30:37 -0300734 fh = kzalloc(sizeof(struct vpif_fh), GFP_KERNEL);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300735 if (fh == NULL) {
736 vpif_err("unable to allocate memory for file handle object\n");
737 return -ENOMEM;
738 }
739
Hans Verkuil0b7286d2012-07-31 03:47:07 -0300740 if (mutex_lock_interruptible(&common->lock)) {
741 kfree(fh);
742 return -ERESTARTSYS;
743 }
Chaithrika U Se7332e32009-06-09 05:55:37 -0300744 /* store pointer to fh in private_data member of filep */
745 filep->private_data = fh;
746 fh->channel = ch;
747 fh->initialized = 0;
748 if (!ch->initialized) {
749 fh->initialized = 1;
750 ch->initialized = 1;
751 memset(&ch->vpifparams, 0, sizeof(ch->vpifparams));
752 }
753
754 /* Increment channel usrs counter */
755 atomic_inc(&ch->usrs);
756 /* Set io_allowed[VPIF_VIDEO_INDEX] member to false */
757 fh->io_allowed[VPIF_VIDEO_INDEX] = 0;
758 /* Initialize priority of this instance to default priority */
759 fh->prio = V4L2_PRIORITY_UNSET;
760 v4l2_prio_open(&ch->prio, &fh->prio);
Hans Verkuil0b7286d2012-07-31 03:47:07 -0300761 mutex_unlock(&common->lock);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300762
763 return 0;
764}
765
766/*
767 * vpif_release: This function deletes buffer queue, frees the buffers and
768 * the vpif file handle
769 */
770static int vpif_release(struct file *filep)
771{
772 struct vpif_fh *fh = filep->private_data;
773 struct channel_obj *ch = fh->channel;
774 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
775
Hans Verkuil0b7286d2012-07-31 03:47:07 -0300776 mutex_lock(&common->lock);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300777 /* if this instance is doing IO */
778 if (fh->io_allowed[VPIF_VIDEO_INDEX]) {
779 /* Reset io_usrs member of channel object */
780 common->io_usrs = 0;
781 /* Disable channel */
782 if (VPIF_CHANNEL2_VIDEO == ch->channel_id) {
783 enable_channel2(0);
784 channel2_intr_enable(0);
785 }
786 if ((VPIF_CHANNEL3_VIDEO == ch->channel_id) ||
787 (2 == common->started)) {
788 enable_channel3(0);
789 channel3_intr_enable(0);
790 }
791 common->started = 0;
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300792
Chaithrika U Se7332e32009-06-09 05:55:37 -0300793 /* Free buffers allocated */
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300794 vb2_queue_release(&common->buffer_queue);
795 vb2_dma_contig_cleanup_ctx(common->alloc_ctx);
796
Chaithrika U Se7332e32009-06-09 05:55:37 -0300797 common->numbuffers =
798 config_params.numbuffers[ch->channel_id];
799 }
800
Chaithrika U Se7332e32009-06-09 05:55:37 -0300801 /* Decrement channel usrs counter */
802 atomic_dec(&ch->usrs);
803 /* If this file handle has initialize encoder device, reset it */
804 if (fh->initialized)
805 ch->initialized = 0;
806
807 /* Close the priority */
Hans Verkuilffb48772010-05-01 08:03:24 -0300808 v4l2_prio_close(&ch->prio, fh->prio);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300809 filep->private_data = NULL;
810 fh->initialized = 0;
Hans Verkuil0b7286d2012-07-31 03:47:07 -0300811 mutex_unlock(&common->lock);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300812 kfree(fh);
813
814 return 0;
815}
816
817/* functions implementing ioctls */
Mats Randgaard2c0ddd12010-12-16 12:17:45 -0300818/**
819 * vpif_querycap() - QUERYCAP handler
820 * @file: file ptr
821 * @priv: file handle
822 * @cap: ptr to v4l2_capability structure
823 */
Chaithrika U Se7332e32009-06-09 05:55:37 -0300824static int vpif_querycap(struct file *file, void *priv,
825 struct v4l2_capability *cap)
826{
Muralidharan Karicheri317b2e22009-09-16 14:30:53 -0300827 struct vpif_display_config *config = vpif_dev->platform_data;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300828
Chaithrika U Se7332e32009-06-09 05:55:37 -0300829 cap->capabilities = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING;
830 strlcpy(cap->driver, "vpif display", sizeof(cap->driver));
831 strlcpy(cap->bus_info, "Platform", sizeof(cap->bus_info));
832 strlcpy(cap->card, config->card_name, sizeof(cap->card));
833
834 return 0;
835}
836
837static int vpif_enum_fmt_vid_out(struct file *file, void *priv,
838 struct v4l2_fmtdesc *fmt)
839{
840 if (fmt->index != 0) {
841 vpif_err("Invalid format index\n");
842 return -EINVAL;
843 }
844
845 /* Fill in the information about format */
846 fmt->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
847 strcpy(fmt->description, "YCbCr4:2:2 YC Planar");
848 fmt->pixelformat = V4L2_PIX_FMT_YUV422P;
849
850 return 0;
851}
852
853static int vpif_g_fmt_vid_out(struct file *file, void *priv,
854 struct v4l2_format *fmt)
855{
856 struct vpif_fh *fh = priv;
857 struct channel_obj *ch = fh->channel;
858 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
859
860 /* Check the validity of the buffer type */
861 if (common->fmt.type != fmt->type)
862 return -EINVAL;
863
Mats Randgaardc027e162010-12-16 12:17:44 -0300864 if (vpif_update_resolution(ch))
Hans Verkuil9bfaae22011-01-05 13:35:45 -0300865 return -EINVAL;
866 *fmt = common->fmt;
867 return 0;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300868}
869
870static int vpif_s_fmt_vid_out(struct file *file, void *priv,
871 struct v4l2_format *fmt)
872{
873 struct vpif_fh *fh = priv;
874 struct v4l2_pix_format *pixfmt;
875 struct channel_obj *ch = fh->channel;
876 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
877 int ret = 0;
878
879 if ((VPIF_CHANNEL2_VIDEO == ch->channel_id)
880 || (VPIF_CHANNEL3_VIDEO == ch->channel_id)) {
881 if (!fh->initialized) {
882 vpif_dbg(1, debug, "Channel Busy\n");
883 return -EBUSY;
884 }
885
886 /* Check for the priority */
Hans Verkuilffb48772010-05-01 08:03:24 -0300887 ret = v4l2_prio_check(&ch->prio, fh->prio);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300888 if (0 != ret)
889 return ret;
890 fh->initialized = 1;
891 }
892
893 if (common->started) {
894 vpif_dbg(1, debug, "Streaming in progress\n");
895 return -EBUSY;
896 }
897
898 pixfmt = &fmt->fmt.pix;
899 /* Check for valid field format */
900 ret = vpif_check_format(ch, pixfmt);
901 if (ret)
902 return ret;
903
904 /* store the pix format in the channel object */
905 common->fmt.fmt.pix = *pixfmt;
906 /* store the format in the channel object */
Chaithrika U Se7332e32009-06-09 05:55:37 -0300907 common->fmt = *fmt;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300908 return 0;
909}
910
911static int vpif_try_fmt_vid_out(struct file *file, void *priv,
912 struct v4l2_format *fmt)
913{
914 struct vpif_fh *fh = priv;
915 struct channel_obj *ch = fh->channel;
916 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
917 struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
918 int ret = 0;
919
920 ret = vpif_check_format(ch, pixfmt);
921 if (ret) {
922 *pixfmt = common->fmt.fmt.pix;
923 pixfmt->sizeimage = pixfmt->width * pixfmt->height * 2;
924 }
925
926 return ret;
927}
928
929static int vpif_reqbufs(struct file *file, void *priv,
930 struct v4l2_requestbuffers *reqbuf)
931{
932 struct vpif_fh *fh = priv;
933 struct channel_obj *ch = fh->channel;
934 struct common_obj *common;
935 enum v4l2_field field;
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300936 struct vb2_queue *q;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300937 u8 index = 0;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300938
939 /* This file handle has not initialized the channel,
940 It is not allowed to do settings */
941 if ((VPIF_CHANNEL2_VIDEO == ch->channel_id)
942 || (VPIF_CHANNEL3_VIDEO == ch->channel_id)) {
943 if (!fh->initialized) {
944 vpif_err("Channel Busy\n");
945 return -EBUSY;
946 }
947 }
948
949 if (V4L2_BUF_TYPE_VIDEO_OUTPUT != reqbuf->type)
950 return -EINVAL;
951
952 index = VPIF_VIDEO_INDEX;
953
954 common = &ch->common[index];
Chaithrika U S13df4f62009-06-22 09:02:55 -0300955
Manjunath Hadlifc613d42012-04-13 04:49:10 -0300956 if (common->fmt.type != reqbuf->type || !vpif_dev)
Hans Verkuil9bfaae22011-01-05 13:35:45 -0300957 return -EINVAL;
Hans Verkuil9bfaae22011-01-05 13:35:45 -0300958 if (0 != common->io_usrs)
959 return -EBUSY;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300960
961 if (reqbuf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
962 if (common->fmt.fmt.pix.field == V4L2_FIELD_ANY)
963 field = V4L2_FIELD_INTERLACED;
964 else
965 field = common->fmt.fmt.pix.field;
966 } else {
967 field = V4L2_VBI_INTERLACED;
968 }
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300969 /* Initialize videobuf2 queue as per the buffer type */
970 common->alloc_ctx = vb2_dma_contig_init_ctx(vpif_dev);
971 if (!common->alloc_ctx) {
972 vpif_err("Failed to get the context\n");
973 return -EINVAL;
974 }
975 q = &common->buffer_queue;
976 q->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
977 q->io_modes = VB2_MMAP | VB2_USERPTR;
978 q->drv_priv = fh;
979 q->ops = &video_qops;
980 q->mem_ops = &vb2_dma_contig_memops;
981 q->buf_struct_size = sizeof(struct vpif_disp_buffer);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300982
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300983 vb2_queue_init(q);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300984
985 /* Set io allowed member of file handle to TRUE */
986 fh->io_allowed[index] = 1;
987 /* Increment io usrs member of channel object to 1 */
988 common->io_usrs = 1;
989 /* Store type of memory requested in channel object */
990 common->memory = reqbuf->memory;
991 INIT_LIST_HEAD(&common->dma_queue);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300992 /* Allocate buffers */
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300993 return vb2_reqbufs(&common->buffer_queue, reqbuf);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300994}
995
996static int vpif_querybuf(struct file *file, void *priv,
997 struct v4l2_buffer *tbuf)
998{
999 struct vpif_fh *fh = priv;
1000 struct channel_obj *ch = fh->channel;
1001 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1002
1003 if (common->fmt.type != tbuf->type)
1004 return -EINVAL;
1005
Lad, Prabhakar2401dd22012-06-28 09:28:36 -03001006 return vb2_querybuf(&common->buffer_queue, tbuf);
Chaithrika U Se7332e32009-06-09 05:55:37 -03001007}
1008
1009static int vpif_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
1010{
Lad, Prabhakar2401dd22012-06-28 09:28:36 -03001011 struct vpif_fh *fh = NULL;
1012 struct channel_obj *ch = NULL;
1013 struct common_obj *common = NULL;
Chaithrika U Se7332e32009-06-09 05:55:37 -03001014
Lad, Prabhakar2401dd22012-06-28 09:28:36 -03001015 if (!buf || !priv)
1016 return -EINVAL;
Chaithrika U Se7332e32009-06-09 05:55:37 -03001017
Lad, Prabhakar2401dd22012-06-28 09:28:36 -03001018 fh = priv;
1019 ch = fh->channel;
1020 if (!ch)
1021 return -EINVAL;
1022
1023 common = &(ch->common[VPIF_VIDEO_INDEX]);
1024 if (common->fmt.type != buf->type)
Chaithrika U Se7332e32009-06-09 05:55:37 -03001025 return -EINVAL;
1026
1027 if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
1028 vpif_err("fh->io_allowed\n");
1029 return -EACCES;
1030 }
1031
Lad, Prabhakar2401dd22012-06-28 09:28:36 -03001032 return vb2_qbuf(&common->buffer_queue, buf);
Chaithrika U Se7332e32009-06-09 05:55:37 -03001033}
1034
1035static int vpif_s_std(struct file *file, void *priv, v4l2_std_id *std_id)
1036{
1037 struct vpif_fh *fh = priv;
1038 struct channel_obj *ch = fh->channel;
1039 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1040 int ret = 0;
1041
Manjunath Hadli0a631722012-04-13 04:44:00 -03001042 if (!(*std_id & VPIF_V4L2_STD))
Chaithrika U Se7332e32009-06-09 05:55:37 -03001043 return -EINVAL;
1044
1045 if (common->started) {
1046 vpif_err("streaming in progress\n");
1047 return -EBUSY;
1048 }
1049
1050 /* Call encoder subdevice function to set the standard */
Chaithrika U Se7332e32009-06-09 05:55:37 -03001051 ch->video.stdid = *std_id;
Hans Verkuil0598c172012-09-18 07:18:47 -03001052 memset(&ch->video.dv_timings, 0, sizeof(ch->video.dv_timings));
Chaithrika U Se7332e32009-06-09 05:55:37 -03001053 /* Get the information about the standard */
Hans Verkuil9bfaae22011-01-05 13:35:45 -03001054 if (vpif_update_resolution(ch))
1055 return -EINVAL;
Chaithrika U Se7332e32009-06-09 05:55:37 -03001056
1057 if ((ch->vpifparams.std_info.width *
1058 ch->vpifparams.std_info.height * 2) >
1059 config_params.channel_bufsize[ch->channel_id]) {
1060 vpif_err("invalid std for this size\n");
Hans Verkuil9bfaae22011-01-05 13:35:45 -03001061 return -EINVAL;
Chaithrika U Se7332e32009-06-09 05:55:37 -03001062 }
1063
1064 common->fmt.fmt.pix.bytesperline = common->fmt.fmt.pix.width;
1065 /* Configure the default format information */
1066 vpif_config_format(ch);
1067
1068 ret = v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 1, video,
1069 s_std_output, *std_id);
1070 if (ret < 0) {
1071 vpif_err("Failed to set output standard\n");
Hans Verkuil9bfaae22011-01-05 13:35:45 -03001072 return ret;
Chaithrika U Se7332e32009-06-09 05:55:37 -03001073 }
1074
1075 ret = v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 1, core,
1076 s_std, *std_id);
1077 if (ret < 0)
1078 vpif_err("Failed to set standard for sub devices\n");
Chaithrika U Se7332e32009-06-09 05:55:37 -03001079 return ret;
1080}
1081
1082static int vpif_g_std(struct file *file, void *priv, v4l2_std_id *std)
1083{
1084 struct vpif_fh *fh = priv;
1085 struct channel_obj *ch = fh->channel;
1086
1087 *std = ch->video.stdid;
1088 return 0;
1089}
1090
1091static int vpif_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
1092{
1093 struct vpif_fh *fh = priv;
1094 struct channel_obj *ch = fh->channel;
1095 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1096
Lad, Prabhakar2401dd22012-06-28 09:28:36 -03001097 return vb2_dqbuf(&common->buffer_queue, p,
Chaithrika U Se7332e32009-06-09 05:55:37 -03001098 (file->f_flags & O_NONBLOCK));
1099}
1100
1101static int vpif_streamon(struct file *file, void *priv,
1102 enum v4l2_buf_type buftype)
1103{
1104 struct vpif_fh *fh = priv;
1105 struct channel_obj *ch = fh->channel;
1106 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1107 struct channel_obj *oth_ch = vpif_obj.dev[!ch->channel_id];
Chaithrika U Se7332e32009-06-09 05:55:37 -03001108 int ret = 0;
1109
1110 if (buftype != V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1111 vpif_err("buffer type not supported\n");
1112 return -EINVAL;
1113 }
1114
1115 if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
1116 vpif_err("fh->io_allowed\n");
1117 return -EACCES;
1118 }
1119
1120 /* If Streaming is already started, return error */
1121 if (common->started) {
1122 vpif_err("channel->started\n");
1123 return -EBUSY;
1124 }
1125
1126 if ((ch->channel_id == VPIF_CHANNEL2_VIDEO
1127 && oth_ch->common[VPIF_VIDEO_INDEX].started &&
1128 ch->vpifparams.std_info.ycmux_mode == 0)
1129 || ((ch->channel_id == VPIF_CHANNEL3_VIDEO)
1130 && (2 == oth_ch->common[VPIF_VIDEO_INDEX].started))) {
1131 vpif_err("other channel is using\n");
1132 return -EBUSY;
1133 }
1134
1135 ret = vpif_check_format(ch, &common->fmt.fmt.pix);
1136 if (ret < 0)
1137 return ret;
1138
Lad, Prabhakar2401dd22012-06-28 09:28:36 -03001139 /* Call vb2_streamon to start streaming in videobuf2 */
1140 ret = vb2_streamon(&common->buffer_queue, buftype);
Chaithrika U Se7332e32009-06-09 05:55:37 -03001141 if (ret < 0) {
Lad, Prabhakar2401dd22012-06-28 09:28:36 -03001142 vpif_err("vb2_streamon\n");
Chaithrika U Se7332e32009-06-09 05:55:37 -03001143 return ret;
1144 }
1145
Chaithrika U Se7332e32009-06-09 05:55:37 -03001146 return ret;
1147}
1148
1149static int vpif_streamoff(struct file *file, void *priv,
1150 enum v4l2_buf_type buftype)
1151{
1152 struct vpif_fh *fh = priv;
1153 struct channel_obj *ch = fh->channel;
1154 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
Manjunath Hadli6964b102012-06-29 03:20:12 -03001155 struct vpif_display_config *vpif_config_data =
1156 vpif_dev->platform_data;
Chaithrika U Se7332e32009-06-09 05:55:37 -03001157
1158 if (buftype != V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1159 vpif_err("buffer type not supported\n");
1160 return -EINVAL;
1161 }
1162
1163 if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
1164 vpif_err("fh->io_allowed\n");
1165 return -EACCES;
1166 }
1167
1168 if (!common->started) {
1169 vpif_err("channel->started\n");
1170 return -EINVAL;
1171 }
1172
Chaithrika U Se7332e32009-06-09 05:55:37 -03001173 if (buftype == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1174 /* disable channel */
1175 if (VPIF_CHANNEL2_VIDEO == ch->channel_id) {
Manjunath Hadli6964b102012-06-29 03:20:12 -03001176 if (vpif_config_data->ch2_clip_en)
1177 channel2_clipping_enable(0);
Chaithrika U Se7332e32009-06-09 05:55:37 -03001178 enable_channel2(0);
1179 channel2_intr_enable(0);
1180 }
1181 if ((VPIF_CHANNEL3_VIDEO == ch->channel_id) ||
1182 (2 == common->started)) {
Manjunath Hadli6964b102012-06-29 03:20:12 -03001183 if (vpif_config_data->ch3_clip_en)
1184 channel3_clipping_enable(0);
Chaithrika U Se7332e32009-06-09 05:55:37 -03001185 enable_channel3(0);
1186 channel3_intr_enable(0);
1187 }
1188 }
1189
1190 common->started = 0;
Lad, Prabhakar2401dd22012-06-28 09:28:36 -03001191 return vb2_streamoff(&common->buffer_queue, buftype);
Chaithrika U Se7332e32009-06-09 05:55:37 -03001192}
1193
1194static int vpif_cropcap(struct file *file, void *priv,
1195 struct v4l2_cropcap *crop)
1196{
1197 struct vpif_fh *fh = priv;
1198 struct channel_obj *ch = fh->channel;
1199 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1200 if (V4L2_BUF_TYPE_VIDEO_OUTPUT != crop->type)
1201 return -EINVAL;
1202
1203 crop->bounds.left = crop->bounds.top = 0;
1204 crop->defrect.left = crop->defrect.top = 0;
1205 crop->defrect.height = crop->bounds.height = common->height;
1206 crop->defrect.width = crop->bounds.width = common->width;
1207
1208 return 0;
1209}
1210
1211static int vpif_enum_output(struct file *file, void *fh,
1212 struct v4l2_output *output)
1213{
1214
Muralidharan Karicheri317b2e22009-09-16 14:30:53 -03001215 struct vpif_display_config *config = vpif_dev->platform_data;
Chaithrika U Se7332e32009-06-09 05:55:37 -03001216
1217 if (output->index >= config->output_count) {
1218 vpif_dbg(1, debug, "Invalid output index\n");
1219 return -EINVAL;
1220 }
1221
1222 strcpy(output->name, config->output[output->index]);
1223 output->type = V4L2_OUTPUT_TYPE_ANALOG;
Manjunath Hadli0a631722012-04-13 04:44:00 -03001224 output->std = VPIF_V4L2_STD;
Chaithrika U Se7332e32009-06-09 05:55:37 -03001225
1226 return 0;
1227}
1228
1229static int vpif_s_output(struct file *file, void *priv, unsigned int i)
1230{
1231 struct vpif_fh *fh = priv;
1232 struct channel_obj *ch = fh->channel;
1233 struct video_obj *vid_ch = &ch->video;
1234 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1235 int ret = 0;
1236
Chaithrika U Se7332e32009-06-09 05:55:37 -03001237 if (common->started) {
1238 vpif_err("Streaming in progress\n");
Hans Verkuil9bfaae22011-01-05 13:35:45 -03001239 return -EBUSY;
Chaithrika U Se7332e32009-06-09 05:55:37 -03001240 }
1241
1242 ret = v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 1, video,
1243 s_routing, 0, i, 0);
1244
1245 if (ret < 0)
1246 vpif_err("Failed to set output standard\n");
1247
1248 vid_ch->output_id = i;
Chaithrika U Se7332e32009-06-09 05:55:37 -03001249 return ret;
1250}
1251
1252static int vpif_g_output(struct file *file, void *priv, unsigned int *i)
1253{
1254 struct vpif_fh *fh = priv;
1255 struct channel_obj *ch = fh->channel;
1256 struct video_obj *vid_ch = &ch->video;
1257
1258 *i = vid_ch->output_id;
1259
1260 return 0;
1261}
1262
1263static int vpif_g_priority(struct file *file, void *priv, enum v4l2_priority *p)
1264{
1265 struct vpif_fh *fh = priv;
1266 struct channel_obj *ch = fh->channel;
1267
1268 *p = v4l2_prio_max(&ch->prio);
1269
1270 return 0;
1271}
1272
1273static int vpif_s_priority(struct file *file, void *priv, enum v4l2_priority p)
1274{
1275 struct vpif_fh *fh = priv;
1276 struct channel_obj *ch = fh->channel;
1277
1278 return v4l2_prio_change(&ch->prio, &fh->prio, p);
1279}
1280
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001281/**
Hans Verkuil0598c172012-09-18 07:18:47 -03001282 * vpif_enum_dv_timings() - ENUM_DV_TIMINGS handler
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001283 * @file: file ptr
1284 * @priv: file handle
Hans Verkuil0598c172012-09-18 07:18:47 -03001285 * @timings: input timings
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001286 */
Hans Verkuil0598c172012-09-18 07:18:47 -03001287static int
1288vpif_enum_dv_timings(struct file *file, void *priv,
1289 struct v4l2_enum_dv_timings *timings)
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001290{
1291 struct vpif_fh *fh = priv;
1292 struct channel_obj *ch = fh->channel;
1293 struct video_obj *vid_ch = &ch->video;
1294
1295 return v4l2_subdev_call(vpif_obj.sd[vid_ch->output_id],
Hans Verkuil0598c172012-09-18 07:18:47 -03001296 video, enum_dv_timings, timings);
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001297}
1298
1299/**
Mats Randgaardc027e162010-12-16 12:17:44 -03001300 * vpif_s_dv_timings() - S_DV_TIMINGS handler
1301 * @file: file ptr
1302 * @priv: file handle
1303 * @timings: digital video timings
1304 */
1305static int vpif_s_dv_timings(struct file *file, void *priv,
1306 struct v4l2_dv_timings *timings)
1307{
1308 struct vpif_fh *fh = priv;
1309 struct channel_obj *ch = fh->channel;
1310 struct vpif_params *vpifparams = &ch->vpifparams;
1311 struct vpif_channel_config_params *std_info = &vpifparams->std_info;
1312 struct video_obj *vid_ch = &ch->video;
Hans Verkuil0598c172012-09-18 07:18:47 -03001313 struct v4l2_bt_timings *bt = &vid_ch->dv_timings.bt;
Mats Randgaardc027e162010-12-16 12:17:44 -03001314 int ret;
1315
1316 if (timings->type != V4L2_DV_BT_656_1120) {
1317 vpif_dbg(2, debug, "Timing type not defined\n");
1318 return -EINVAL;
1319 }
1320
1321 /* Configure subdevice timings, if any */
1322 ret = v4l2_subdev_call(vpif_obj.sd[vid_ch->output_id],
1323 video, s_dv_timings, timings);
1324 if (ret == -ENOIOCTLCMD) {
1325 vpif_dbg(2, debug, "Custom DV timings not supported by "
1326 "subdevice\n");
1327 return -EINVAL;
1328 }
1329 if (ret < 0) {
1330 vpif_dbg(2, debug, "Error setting custom DV timings\n");
1331 return ret;
1332 }
1333
1334 if (!(timings->bt.width && timings->bt.height &&
1335 (timings->bt.hbackporch ||
1336 timings->bt.hfrontporch ||
1337 timings->bt.hsync) &&
1338 timings->bt.vfrontporch &&
1339 (timings->bt.vbackporch ||
1340 timings->bt.vsync))) {
1341 vpif_dbg(2, debug, "Timings for width, height, "
1342 "horizontal back porch, horizontal sync, "
1343 "horizontal front porch, vertical back porch, "
1344 "vertical sync and vertical back porch "
1345 "must be defined\n");
1346 return -EINVAL;
1347 }
1348
Hans Verkuil0598c172012-09-18 07:18:47 -03001349 vid_ch->dv_timings = *timings;
Mats Randgaardc027e162010-12-16 12:17:44 -03001350
1351 /* Configure video port timings */
1352
1353 std_info->eav2sav = bt->hbackporch + bt->hfrontporch +
1354 bt->hsync - 8;
1355 std_info->sav2eav = bt->width;
1356
1357 std_info->l1 = 1;
1358 std_info->l3 = bt->vsync + bt->vbackporch + 1;
1359
1360 if (bt->interlaced) {
1361 if (bt->il_vbackporch || bt->il_vfrontporch || bt->il_vsync) {
1362 std_info->vsize = bt->height * 2 +
1363 bt->vfrontporch + bt->vsync + bt->vbackporch +
1364 bt->il_vfrontporch + bt->il_vsync +
1365 bt->il_vbackporch;
1366 std_info->l5 = std_info->vsize/2 -
1367 (bt->vfrontporch - 1);
1368 std_info->l7 = std_info->vsize/2 + 1;
1369 std_info->l9 = std_info->l7 + bt->il_vsync +
1370 bt->il_vbackporch + 1;
1371 std_info->l11 = std_info->vsize -
1372 (bt->il_vfrontporch - 1);
1373 } else {
1374 vpif_dbg(2, debug, "Required timing values for "
1375 "interlaced BT format missing\n");
1376 return -EINVAL;
1377 }
1378 } else {
1379 std_info->vsize = bt->height + bt->vfrontporch +
1380 bt->vsync + bt->vbackporch;
1381 std_info->l5 = std_info->vsize - (bt->vfrontporch - 1);
1382 }
1383 strncpy(std_info->name, "Custom timings BT656/1120",
1384 VPIF_MAX_NAME);
1385 std_info->width = bt->width;
1386 std_info->height = bt->height;
1387 std_info->frm_fmt = bt->interlaced ? 0 : 1;
1388 std_info->ycmux_mode = 0;
1389 std_info->capture_format = 0;
1390 std_info->vbi_supported = 0;
1391 std_info->hd_sd = 1;
1392 std_info->stdid = 0;
Mats Randgaardc027e162010-12-16 12:17:44 -03001393 vid_ch->stdid = 0;
Mats Randgaardc027e162010-12-16 12:17:44 -03001394
1395 return 0;
1396}
1397
1398/**
1399 * vpif_g_dv_timings() - G_DV_TIMINGS handler
1400 * @file: file ptr
1401 * @priv: file handle
1402 * @timings: digital video timings
1403 */
1404static int vpif_g_dv_timings(struct file *file, void *priv,
1405 struct v4l2_dv_timings *timings)
1406{
1407 struct vpif_fh *fh = priv;
1408 struct channel_obj *ch = fh->channel;
1409 struct video_obj *vid_ch = &ch->video;
Mats Randgaardc027e162010-12-16 12:17:44 -03001410
Hans Verkuil0598c172012-09-18 07:18:47 -03001411 *timings = vid_ch->dv_timings;
Mats Randgaardc027e162010-12-16 12:17:44 -03001412
1413 return 0;
1414}
Mats Randgaard7036d6a2010-12-16 12:17:41 -03001415
1416/*
1417 * vpif_g_chip_ident() - Identify the chip
1418 * @file: file ptr
1419 * @priv: file handle
1420 * @chip: chip identity
1421 *
1422 * Returns zero or -EINVAL if read operations fails.
1423 */
1424static int vpif_g_chip_ident(struct file *file, void *priv,
1425 struct v4l2_dbg_chip_ident *chip)
1426{
1427 chip->ident = V4L2_IDENT_NONE;
1428 chip->revision = 0;
1429 if (chip->match.type != V4L2_CHIP_MATCH_I2C_DRIVER &&
1430 chip->match.type != V4L2_CHIP_MATCH_I2C_ADDR) {
1431 vpif_dbg(2, debug, "match_type is invalid.\n");
1432 return -EINVAL;
1433 }
1434
1435 return v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 0, core,
1436 g_chip_ident, chip);
1437}
1438
1439#ifdef CONFIG_VIDEO_ADV_DEBUG
1440/*
1441 * vpif_dbg_g_register() - Read register
1442 * @file: file ptr
1443 * @priv: file handle
1444 * @reg: register to be read
1445 *
1446 * Debugging only
1447 * Returns zero or -EINVAL if read operations fails.
1448 */
1449static int vpif_dbg_g_register(struct file *file, void *priv,
1450 struct v4l2_dbg_register *reg){
1451 struct vpif_fh *fh = priv;
1452 struct channel_obj *ch = fh->channel;
1453 struct video_obj *vid_ch = &ch->video;
1454
1455 return v4l2_subdev_call(vpif_obj.sd[vid_ch->output_id], core,
1456 g_register, reg);
1457}
1458
1459/*
1460 * vpif_dbg_s_register() - Write to register
1461 * @file: file ptr
1462 * @priv: file handle
1463 * @reg: register to be modified
1464 *
1465 * Debugging only
1466 * Returns zero or -EINVAL if write operations fails.
1467 */
1468static int vpif_dbg_s_register(struct file *file, void *priv,
1469 struct v4l2_dbg_register *reg){
1470 struct vpif_fh *fh = priv;
1471 struct channel_obj *ch = fh->channel;
1472 struct video_obj *vid_ch = &ch->video;
1473
1474 return v4l2_subdev_call(vpif_obj.sd[vid_ch->output_id], core,
1475 s_register, reg);
1476}
1477#endif
1478
1479/*
1480 * vpif_log_status() - Status information
1481 * @file: file ptr
1482 * @priv: file handle
1483 *
1484 * Returns zero.
1485 */
1486static int vpif_log_status(struct file *filep, void *priv)
1487{
1488 /* status for sub devices */
1489 v4l2_device_call_all(&vpif_obj.v4l2_dev, 0, core, log_status);
1490
1491 return 0;
1492}
1493
Chaithrika U Se7332e32009-06-09 05:55:37 -03001494/* vpif display ioctl operations */
1495static const struct v4l2_ioctl_ops vpif_ioctl_ops = {
1496 .vidioc_querycap = vpif_querycap,
1497 .vidioc_g_priority = vpif_g_priority,
1498 .vidioc_s_priority = vpif_s_priority,
1499 .vidioc_enum_fmt_vid_out = vpif_enum_fmt_vid_out,
1500 .vidioc_g_fmt_vid_out = vpif_g_fmt_vid_out,
1501 .vidioc_s_fmt_vid_out = vpif_s_fmt_vid_out,
1502 .vidioc_try_fmt_vid_out = vpif_try_fmt_vid_out,
1503 .vidioc_reqbufs = vpif_reqbufs,
1504 .vidioc_querybuf = vpif_querybuf,
1505 .vidioc_qbuf = vpif_qbuf,
1506 .vidioc_dqbuf = vpif_dqbuf,
1507 .vidioc_streamon = vpif_streamon,
1508 .vidioc_streamoff = vpif_streamoff,
1509 .vidioc_s_std = vpif_s_std,
1510 .vidioc_g_std = vpif_g_std,
1511 .vidioc_enum_output = vpif_enum_output,
1512 .vidioc_s_output = vpif_s_output,
1513 .vidioc_g_output = vpif_g_output,
1514 .vidioc_cropcap = vpif_cropcap,
Hans Verkuil0598c172012-09-18 07:18:47 -03001515 .vidioc_enum_dv_timings = vpif_enum_dv_timings,
Mats Randgaardc027e162010-12-16 12:17:44 -03001516 .vidioc_s_dv_timings = vpif_s_dv_timings,
1517 .vidioc_g_dv_timings = vpif_g_dv_timings,
Mats Randgaard7036d6a2010-12-16 12:17:41 -03001518 .vidioc_g_chip_ident = vpif_g_chip_ident,
1519#ifdef CONFIG_VIDEO_ADV_DEBUG
1520 .vidioc_g_register = vpif_dbg_g_register,
1521 .vidioc_s_register = vpif_dbg_s_register,
1522#endif
1523 .vidioc_log_status = vpif_log_status,
Chaithrika U Se7332e32009-06-09 05:55:37 -03001524};
1525
1526static const struct v4l2_file_operations vpif_fops = {
1527 .owner = THIS_MODULE,
1528 .open = vpif_open,
1529 .release = vpif_release,
Hans Verkuil9bfaae22011-01-05 13:35:45 -03001530 .unlocked_ioctl = video_ioctl2,
Chaithrika U Se7332e32009-06-09 05:55:37 -03001531 .mmap = vpif_mmap,
1532 .poll = vpif_poll
1533};
1534
1535static struct video_device vpif_video_template = {
1536 .name = "vpif",
1537 .fops = &vpif_fops,
Chaithrika U Se7332e32009-06-09 05:55:37 -03001538 .ioctl_ops = &vpif_ioctl_ops,
Manjunath Hadli0a631722012-04-13 04:44:00 -03001539 .tvnorms = VPIF_V4L2_STD,
Chaithrika U Se7332e32009-06-09 05:55:37 -03001540 .current_norm = V4L2_STD_625_50,
1541
1542};
1543
1544/*Configure the channels, buffer sizei, request irq */
1545static int initialize_vpif(void)
1546{
1547 int free_channel_objects_index;
1548 int free_buffer_channel_index;
1549 int free_buffer_index;
1550 int err = 0, i, j;
1551
1552 /* Default number of buffers should be 3 */
1553 if ((ch2_numbuffers > 0) &&
1554 (ch2_numbuffers < config_params.min_numbuffers))
1555 ch2_numbuffers = config_params.min_numbuffers;
1556 if ((ch3_numbuffers > 0) &&
1557 (ch3_numbuffers < config_params.min_numbuffers))
1558 ch3_numbuffers = config_params.min_numbuffers;
1559
1560 /* Set buffer size to min buffers size if invalid buffer size is
1561 * given */
1562 if (ch2_bufsize < config_params.min_bufsize[VPIF_CHANNEL2_VIDEO])
1563 ch2_bufsize =
1564 config_params.min_bufsize[VPIF_CHANNEL2_VIDEO];
1565 if (ch3_bufsize < config_params.min_bufsize[VPIF_CHANNEL3_VIDEO])
1566 ch3_bufsize =
1567 config_params.min_bufsize[VPIF_CHANNEL3_VIDEO];
1568
1569 config_params.numbuffers[VPIF_CHANNEL2_VIDEO] = ch2_numbuffers;
1570
1571 if (ch2_numbuffers) {
1572 config_params.channel_bufsize[VPIF_CHANNEL2_VIDEO] =
1573 ch2_bufsize;
1574 }
1575 config_params.numbuffers[VPIF_CHANNEL3_VIDEO] = ch3_numbuffers;
1576
1577 if (ch3_numbuffers) {
1578 config_params.channel_bufsize[VPIF_CHANNEL3_VIDEO] =
1579 ch3_bufsize;
1580 }
1581
1582 /* Allocate memory for six channel objects */
1583 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1584 vpif_obj.dev[i] =
Mats Randgaard1f8766b2010-08-30 10:30:37 -03001585 kzalloc(sizeof(struct channel_obj), GFP_KERNEL);
Chaithrika U Se7332e32009-06-09 05:55:37 -03001586 /* If memory allocation fails, return error */
1587 if (!vpif_obj.dev[i]) {
1588 free_channel_objects_index = i;
1589 err = -ENOMEM;
1590 goto vpif_init_free_channel_objects;
1591 }
1592 }
1593
1594 free_channel_objects_index = VPIF_DISPLAY_MAX_DEVICES;
1595 free_buffer_channel_index = VPIF_DISPLAY_NUM_CHANNELS;
1596 free_buffer_index = config_params.numbuffers[i - 1];
1597
1598 return 0;
1599
1600vpif_init_free_channel_objects:
1601 for (j = 0; j < free_channel_objects_index; j++)
1602 kfree(vpif_obj.dev[j]);
1603 return err;
1604}
1605
1606/*
1607 * vpif_probe: This function creates device entries by register itself to the
1608 * V4L2 driver and initializes fields of each channel objects
1609 */
1610static __init int vpif_probe(struct platform_device *pdev)
1611{
Muralidharan Karicheri317b2e22009-09-16 14:30:53 -03001612 struct vpif_subdev_info *subdevdata;
1613 struct vpif_display_config *config;
Chaithrika U Se7332e32009-06-09 05:55:37 -03001614 int i, j = 0, k, q, m, err = 0;
1615 struct i2c_adapter *i2c_adap;
Chaithrika U Se7332e32009-06-09 05:55:37 -03001616 struct common_obj *common;
1617 struct channel_obj *ch;
1618 struct video_device *vfd;
1619 struct resource *res;
1620 int subdev_count;
Manjunath Hadlifc613d42012-04-13 04:49:10 -03001621 size_t size;
Chaithrika U Se7332e32009-06-09 05:55:37 -03001622
1623 vpif_dev = &pdev->dev;
Muralidharan Karicheri317b2e22009-09-16 14:30:53 -03001624 err = initialize_vpif();
1625
1626 if (err) {
1627 v4l2_err(vpif_dev->driver, "Error initializing vpif\n");
1628 return err;
Chaithrika U Se7332e32009-06-09 05:55:37 -03001629 }
1630
Chaithrika U Se7332e32009-06-09 05:55:37 -03001631 err = v4l2_device_register(vpif_dev, &vpif_obj.v4l2_dev);
1632 if (err) {
1633 v4l2_err(vpif_dev->driver, "Error registering v4l2 device\n");
1634 return err;
1635 }
1636
1637 k = 0;
1638 while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, k))) {
1639 for (i = res->start; i <= res->end; i++) {
Manjunath Hadli0316b892012-04-13 04:44:31 -03001640 if (request_irq(i, vpif_channel_isr, IRQF_SHARED,
Manjunath Hadli0a631722012-04-13 04:44:00 -03001641 "VPIF_Display",
Chaithrika U Se7332e32009-06-09 05:55:37 -03001642 (void *)(&vpif_obj.dev[k]->channel_id))) {
1643 err = -EBUSY;
1644 goto vpif_int_err;
1645 }
1646 }
1647 k++;
1648 }
1649
1650 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1651
1652 /* Get the pointer to the channel object */
1653 ch = vpif_obj.dev[i];
1654
1655 /* Allocate memory for video device */
1656 vfd = video_device_alloc();
1657 if (vfd == NULL) {
1658 for (j = 0; j < i; j++) {
1659 ch = vpif_obj.dev[j];
1660 video_device_release(ch->video_dev);
1661 }
1662 err = -ENOMEM;
Muralidharan Karicheri317b2e22009-09-16 14:30:53 -03001663 goto vpif_int_err;
Chaithrika U Se7332e32009-06-09 05:55:37 -03001664 }
1665
1666 /* Initialize field of video device */
1667 *vfd = vpif_video_template;
1668 vfd->v4l2_dev = &vpif_obj.v4l2_dev;
1669 vfd->release = video_device_release;
Hans Verkuil954f3402012-09-05 06:05:50 -03001670 vfd->vfl_dir = VFL_DIR_TX;
Chaithrika U Se7332e32009-06-09 05:55:37 -03001671 snprintf(vfd->name, sizeof(vfd->name),
Manjunath Hadli0a631722012-04-13 04:44:00 -03001672 "VPIF_Display_DRIVER_V%s",
Mauro Carvalho Chehab64dc3c12011-06-25 11:28:37 -03001673 VPIF_DISPLAY_VERSION);
Chaithrika U Se7332e32009-06-09 05:55:37 -03001674
1675 /* Set video_dev to the video device */
1676 ch->video_dev = vfd;
1677 }
1678
Manjunath Hadlifc613d42012-04-13 04:49:10 -03001679 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1680 if (res) {
1681 size = resource_size(res);
1682 /* The resources are divided into two equal memory and when
1683 * we have HD output we can add them together
1684 */
1685 for (j = 0; j < VPIF_DISPLAY_MAX_DEVICES; j++) {
1686 ch = vpif_obj.dev[j];
1687 ch->channel_id = j;
1688
1689 /* only enabled if second resource exists */
1690 config_params.video_limit[ch->channel_id] = 0;
1691 if (size)
1692 config_params.video_limit[ch->channel_id] =
1693 size/2;
1694 }
1695 }
1696
Chaithrika U Se7332e32009-06-09 05:55:37 -03001697 for (j = 0; j < VPIF_DISPLAY_MAX_DEVICES; j++) {
1698 ch = vpif_obj.dev[j];
1699 /* Initialize field of the channel objects */
1700 atomic_set(&ch->usrs, 0);
1701 for (k = 0; k < VPIF_NUMOBJECTS; k++) {
1702 ch->common[k].numbuffers = 0;
1703 common = &ch->common[k];
1704 common->io_usrs = 0;
1705 common->started = 0;
1706 spin_lock_init(&common->irqlock);
1707 mutex_init(&common->lock);
1708 common->numbuffers = 0;
1709 common->set_addr = NULL;
1710 common->ytop_off = common->ybtm_off = 0;
1711 common->ctop_off = common->cbtm_off = 0;
1712 common->cur_frm = common->next_frm = NULL;
1713 memset(&common->fmt, 0, sizeof(common->fmt));
1714 common->numbuffers = config_params.numbuffers[k];
1715
1716 }
1717 ch->initialized = 0;
1718 ch->channel_id = j;
1719 if (j < 2)
1720 ch->common[VPIF_VIDEO_INDEX].numbuffers =
1721 config_params.numbuffers[ch->channel_id];
1722 else
1723 ch->common[VPIF_VIDEO_INDEX].numbuffers = 0;
1724
1725 memset(&ch->vpifparams, 0, sizeof(ch->vpifparams));
1726
1727 /* Initialize prio member of channel object */
1728 v4l2_prio_init(&ch->prio);
1729 ch->common[VPIF_VIDEO_INDEX].fmt.type =
1730 V4L2_BUF_TYPE_VIDEO_OUTPUT;
Hans Verkuil9bfaae22011-01-05 13:35:45 -03001731 ch->video_dev->lock = &common->lock;
Chaithrika U Se7332e32009-06-09 05:55:37 -03001732
1733 /* register video device */
1734 vpif_dbg(1, debug, "channel=%x,channel->video_dev=%x\n",
1735 (int)ch, (int)&ch->video_dev);
1736
1737 err = video_register_device(ch->video_dev,
1738 VFL_TYPE_GRABBER, (j ? 3 : 2));
1739 if (err < 0)
1740 goto probe_out;
1741
1742 video_set_drvdata(ch->video_dev, ch);
1743 }
1744
1745 i2c_adap = i2c_get_adapter(1);
1746 config = pdev->dev.platform_data;
1747 subdev_count = config->subdev_count;
1748 subdevdata = config->subdevinfo;
Mats Randgaard1f8766b2010-08-30 10:30:37 -03001749 vpif_obj.sd = kzalloc(sizeof(struct v4l2_subdev *) * subdev_count,
Chaithrika U Se7332e32009-06-09 05:55:37 -03001750 GFP_KERNEL);
1751 if (vpif_obj.sd == NULL) {
1752 vpif_err("unable to allocate memory for subdevice pointers\n");
1753 err = -ENOMEM;
1754 goto probe_out;
1755 }
1756
1757 for (i = 0; i < subdev_count; i++) {
Muralidharan Karicheri317b2e22009-09-16 14:30:53 -03001758 vpif_obj.sd[i] = v4l2_i2c_new_subdev_board(&vpif_obj.v4l2_dev,
Laurent Pinchart9a1f8b32010-09-24 10:16:44 -03001759 i2c_adap,
Muralidharan Karicheri317b2e22009-09-16 14:30:53 -03001760 &subdevdata[i].board_info,
1761 NULL);
Chaithrika U Se7332e32009-06-09 05:55:37 -03001762 if (!vpif_obj.sd[i]) {
1763 vpif_err("Error registering v4l2 subdevice\n");
1764 goto probe_subdev_out;
1765 }
1766
1767 if (vpif_obj.sd[i])
1768 vpif_obj.sd[i]->grp_id = 1 << i;
1769 }
1770
Mats Randgaard2c0ddd12010-12-16 12:17:45 -03001771 v4l2_info(&vpif_obj.v4l2_dev,
Manjunath Hadli0a631722012-04-13 04:44:00 -03001772 " VPIF display driver initialized\n");
Chaithrika U Se7332e32009-06-09 05:55:37 -03001773 return 0;
1774
1775probe_subdev_out:
1776 kfree(vpif_obj.sd);
1777probe_out:
1778 for (k = 0; k < j; k++) {
1779 ch = vpif_obj.dev[k];
1780 video_unregister_device(ch->video_dev);
1781 video_device_release(ch->video_dev);
1782 ch->video_dev = NULL;
1783 }
1784vpif_int_err:
1785 v4l2_device_unregister(&vpif_obj.v4l2_dev);
1786 vpif_err("VPIF IRQ request failed\n");
1787 for (q = k; k >= 0; k--) {
1788 for (m = i; m >= res->start; m--)
1789 free_irq(m, (void *)(&vpif_obj.dev[k]->channel_id));
1790 res = platform_get_resource(pdev, IORESOURCE_IRQ, k-1);
1791 m = res->end;
1792 }
Chaithrika U Se7332e32009-06-09 05:55:37 -03001793
1794 return err;
1795}
1796
1797/*
1798 * vpif_remove: It un-register channels from V4L2 driver
1799 */
1800static int vpif_remove(struct platform_device *device)
1801{
1802 struct channel_obj *ch;
1803 int i;
1804
1805 v4l2_device_unregister(&vpif_obj.v4l2_dev);
1806
1807 /* un-register device */
1808 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1809 /* Get the pointer to the channel object */
1810 ch = vpif_obj.dev[i];
1811 /* Unregister video device */
1812 video_unregister_device(ch->video_dev);
1813
1814 ch->video_dev = NULL;
1815 }
1816
1817 return 0;
1818}
1819
Manjunath Hadlie9530da2012-04-13 04:50:35 -03001820#ifdef CONFIG_PM
1821static int vpif_suspend(struct device *dev)
1822{
1823 struct common_obj *common;
1824 struct channel_obj *ch;
1825 int i;
1826
1827 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1828 /* Get the pointer to the channel object */
1829 ch = vpif_obj.dev[i];
1830 common = &ch->common[VPIF_VIDEO_INDEX];
1831 mutex_lock(&common->lock);
1832 if (atomic_read(&ch->usrs) && common->io_usrs) {
1833 /* Disable channel */
1834 if (ch->channel_id == VPIF_CHANNEL2_VIDEO) {
1835 enable_channel2(0);
1836 channel2_intr_enable(0);
1837 }
1838 if (ch->channel_id == VPIF_CHANNEL3_VIDEO ||
1839 common->started == 2) {
1840 enable_channel3(0);
1841 channel3_intr_enable(0);
1842 }
1843 }
1844 mutex_unlock(&common->lock);
1845 }
1846
1847 return 0;
1848}
1849
1850static int vpif_resume(struct device *dev)
1851{
1852
1853 struct common_obj *common;
1854 struct channel_obj *ch;
1855 int i;
1856
1857 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1858 /* Get the pointer to the channel object */
1859 ch = vpif_obj.dev[i];
1860 common = &ch->common[VPIF_VIDEO_INDEX];
1861 mutex_lock(&common->lock);
1862 if (atomic_read(&ch->usrs) && common->io_usrs) {
1863 /* Enable channel */
1864 if (ch->channel_id == VPIF_CHANNEL2_VIDEO) {
1865 enable_channel2(1);
1866 channel2_intr_enable(1);
1867 }
1868 if (ch->channel_id == VPIF_CHANNEL3_VIDEO ||
1869 common->started == 2) {
1870 enable_channel3(1);
1871 channel3_intr_enable(1);
1872 }
1873 }
1874 mutex_unlock(&common->lock);
1875 }
1876
1877 return 0;
1878}
1879
1880static const struct dev_pm_ops vpif_pm = {
1881 .suspend = vpif_suspend,
1882 .resume = vpif_resume,
1883};
1884
1885#define vpif_pm_ops (&vpif_pm)
1886#else
1887#define vpif_pm_ops NULL
1888#endif
1889
Mats Randgaardffa1b392010-08-30 10:30:36 -03001890static __refdata struct platform_driver vpif_driver = {
Chaithrika U Se7332e32009-06-09 05:55:37 -03001891 .driver = {
1892 .name = "vpif_display",
1893 .owner = THIS_MODULE,
Manjunath Hadlie9530da2012-04-13 04:50:35 -03001894 .pm = vpif_pm_ops,
Chaithrika U Se7332e32009-06-09 05:55:37 -03001895 },
1896 .probe = vpif_probe,
1897 .remove = vpif_remove,
1898};
1899
1900static __init int vpif_init(void)
1901{
1902 return platform_driver_register(&vpif_driver);
1903}
1904
1905/*
1906 * vpif_cleanup: This function un-registers device and driver to the kernel,
1907 * frees requested irq handler and de-allocates memory allocated for channel
1908 * objects.
1909 */
1910static void vpif_cleanup(void)
1911{
1912 struct platform_device *pdev;
1913 struct resource *res;
1914 int irq_num;
1915 int i = 0;
1916
1917 pdev = container_of(vpif_dev, struct platform_device, dev);
1918
1919 while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, i))) {
1920 for (irq_num = res->start; irq_num <= res->end; irq_num++)
1921 free_irq(irq_num,
1922 (void *)(&vpif_obj.dev[i]->channel_id));
1923 i++;
1924 }
1925
Chaithrika U Se7332e32009-06-09 05:55:37 -03001926 platform_driver_unregister(&vpif_driver);
1927 kfree(vpif_obj.sd);
1928 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++)
1929 kfree(vpif_obj.dev[i]);
1930}
1931
1932module_init(vpif_init);
1933module_exit(vpif_cleanup);