blob: 9f2b603be9c9b319d56b72bd99c3028df07632aa [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;
Hans Verkuilc4697d72012-11-16 12:03:07 -0300180 unsigned long flags;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300181
182 common = &ch->common[VPIF_VIDEO_INDEX];
183
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300184 /* add the buffer to the DMA queue */
Hans Verkuilc4697d72012-11-16 12:03:07 -0300185 spin_lock_irqsave(&common->irqlock, flags);
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300186 list_add_tail(&buf->list, &common->dma_queue);
Hans Verkuilc4697d72012-11-16 12:03:07 -0300187 spin_unlock_irqrestore(&common->irqlock, flags);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300188}
189
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300190/*
191 * vpif_buf_cleanup: This function is called from the videobuf2 layer to
192 * free memory allocated to the buffers
193 */
194static void vpif_buf_cleanup(struct vb2_buffer *vb)
195{
196 struct vpif_fh *fh = vb2_get_drv_priv(vb->vb2_queue);
197 struct vpif_disp_buffer *buf = container_of(vb,
198 struct vpif_disp_buffer, vb);
199 struct channel_obj *ch = fh->channel;
200 struct common_obj *common;
201 unsigned long flags;
202
203 common = &ch->common[VPIF_VIDEO_INDEX];
204
205 spin_lock_irqsave(&common->irqlock, flags);
206 if (vb->state == VB2_BUF_STATE_ACTIVE)
207 list_del_init(&buf->list);
208 spin_unlock_irqrestore(&common->irqlock, flags);
209}
210
211static void vpif_wait_prepare(struct vb2_queue *vq)
212{
213 struct vpif_fh *fh = vb2_get_drv_priv(vq);
214 struct channel_obj *ch = fh->channel;
215 struct common_obj *common;
216
217 common = &ch->common[VPIF_VIDEO_INDEX];
218 mutex_unlock(&common->lock);
219}
220
221static void vpif_wait_finish(struct vb2_queue *vq)
222{
223 struct vpif_fh *fh = vb2_get_drv_priv(vq);
224 struct channel_obj *ch = fh->channel;
225 struct common_obj *common;
226
227 common = &ch->common[VPIF_VIDEO_INDEX];
228 mutex_lock(&common->lock);
229}
230
231static int vpif_buffer_init(struct vb2_buffer *vb)
232{
233 struct vpif_disp_buffer *buf = container_of(vb,
234 struct vpif_disp_buffer, vb);
235
236 INIT_LIST_HEAD(&buf->list);
237
238 return 0;
239}
240
Chaithrika U Se7332e32009-06-09 05:55:37 -0300241static u8 channel_first_int[VPIF_NUMOBJECTS][2] = { {1, 1} };
242
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300243static int vpif_start_streaming(struct vb2_queue *vq, unsigned int count)
244{
245 struct vpif_display_config *vpif_config_data =
246 vpif_dev->platform_data;
247 struct vpif_fh *fh = vb2_get_drv_priv(vq);
248 struct channel_obj *ch = fh->channel;
249 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
250 struct vpif_params *vpif = &ch->vpifparams;
251 unsigned long addr = 0;
Hans Verkuilc4697d72012-11-16 12:03:07 -0300252 unsigned long flags;
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300253 int ret;
254
255 /* If buffer queue is empty, return error */
Hans Verkuilc4697d72012-11-16 12:03:07 -0300256 spin_lock_irqsave(&common->irqlock, flags);
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300257 if (list_empty(&common->dma_queue)) {
Hans Verkuilc4697d72012-11-16 12:03:07 -0300258 spin_unlock_irqrestore(&common->irqlock, flags);
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300259 vpif_err("buffer queue is empty\n");
260 return -EIO;
261 }
262
263 /* Get the next frame from the buffer queue */
264 common->next_frm = common->cur_frm =
265 list_entry(common->dma_queue.next,
266 struct vpif_disp_buffer, list);
267
268 list_del(&common->cur_frm->list);
Hans Verkuilc4697d72012-11-16 12:03:07 -0300269 spin_unlock_irqrestore(&common->irqlock, flags);
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300270 /* Mark state of the current frame to active */
271 common->cur_frm->vb.state = VB2_BUF_STATE_ACTIVE;
272
273 /* Initialize field_id and started member */
274 ch->field_id = 0;
275 common->started = 1;
276 addr = vb2_dma_contig_plane_dma_addr(&common->cur_frm->vb, 0);
277 /* Calculate the offset for Y and C data in the buffer */
278 vpif_calculate_offsets(ch);
279
280 if ((ch->vpifparams.std_info.frm_fmt &&
281 ((common->fmt.fmt.pix.field != V4L2_FIELD_NONE)
282 && (common->fmt.fmt.pix.field != V4L2_FIELD_ANY)))
283 || (!ch->vpifparams.std_info.frm_fmt
284 && (common->fmt.fmt.pix.field == V4L2_FIELD_NONE))) {
285 vpif_err("conflict in field format and std format\n");
286 return -EINVAL;
287 }
288
289 /* clock settings */
Lad, Prabhakarf4ad8d72012-09-27 02:33:12 -0300290 if (vpif_config_data->set_clock) {
291 ret = vpif_config_data->set_clock(ch->vpifparams.std_info.
292 ycmux_mode, ch->vpifparams.std_info.hd_sd);
293 if (ret < 0) {
294 vpif_err("can't set clock\n");
295 return ret;
296 }
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300297 }
298
299 /* set the parameters and addresses */
300 ret = vpif_set_video_params(vpif, ch->channel_id + 2);
301 if (ret < 0)
302 return ret;
303
304 common->started = ret;
305 vpif_config_addr(ch, ret);
306 common->set_addr((addr + common->ytop_off),
307 (addr + common->ybtm_off),
308 (addr + common->ctop_off),
309 (addr + common->cbtm_off));
310
311 /* Set interrupt for both the fields in VPIF
312 Register enable channel in VPIF register */
Lad, Prabhakar9e184042012-09-14 10:22:24 -0300313 channel_first_int[VPIF_VIDEO_INDEX][ch->channel_id] = 1;
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300314 if (VPIF_CHANNEL2_VIDEO == ch->channel_id) {
315 channel2_intr_assert();
316 channel2_intr_enable(1);
317 enable_channel2(1);
Lad, Prabhakar2bd4e582012-09-25 08:11:49 -0300318 if (vpif_config_data->chan_config[VPIF_CHANNEL2_VIDEO].clip_en)
Manjunath Hadli6964b102012-06-29 03:20:12 -0300319 channel2_clipping_enable(1);
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300320 }
321
322 if ((VPIF_CHANNEL3_VIDEO == ch->channel_id)
323 || (common->started == 2)) {
324 channel3_intr_assert();
325 channel3_intr_enable(1);
326 enable_channel3(1);
Lad, Prabhakar2bd4e582012-09-25 08:11:49 -0300327 if (vpif_config_data->chan_config[VPIF_CHANNEL3_VIDEO].clip_en)
Manjunath Hadli6964b102012-06-29 03:20:12 -0300328 channel3_clipping_enable(1);
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300329 }
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300330
331 return 0;
332}
333
334/* abort streaming and wait for last buffer */
335static int vpif_stop_streaming(struct vb2_queue *vq)
336{
337 struct vpif_fh *fh = vb2_get_drv_priv(vq);
338 struct channel_obj *ch = fh->channel;
339 struct common_obj *common;
Hans Verkuilc4697d72012-11-16 12:03:07 -0300340 unsigned long flags;
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300341
342 if (!vb2_is_streaming(vq))
343 return 0;
344
345 common = &ch->common[VPIF_VIDEO_INDEX];
346
347 /* release all active buffers */
Hans Verkuilc4697d72012-11-16 12:03:07 -0300348 spin_lock_irqsave(&common->irqlock, flags);
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300349 while (!list_empty(&common->dma_queue)) {
350 common->next_frm = list_entry(common->dma_queue.next,
351 struct vpif_disp_buffer, list);
352 list_del(&common->next_frm->list);
353 vb2_buffer_done(&common->next_frm->vb, VB2_BUF_STATE_ERROR);
354 }
Hans Verkuilc4697d72012-11-16 12:03:07 -0300355 spin_unlock_irqrestore(&common->irqlock, flags);
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300356
357 return 0;
358}
359
360static struct vb2_ops video_qops = {
361 .queue_setup = vpif_buffer_queue_setup,
362 .wait_prepare = vpif_wait_prepare,
363 .wait_finish = vpif_wait_finish,
364 .buf_init = vpif_buffer_init,
365 .buf_prepare = vpif_buffer_prepare,
366 .start_streaming = vpif_start_streaming,
367 .stop_streaming = vpif_stop_streaming,
368 .buf_cleanup = vpif_buf_cleanup,
369 .buf_queue = vpif_buffer_queue,
370};
371
Chaithrika U Se7332e32009-06-09 05:55:37 -0300372static void process_progressive_mode(struct common_obj *common)
373{
374 unsigned long addr = 0;
375
Hans Verkuilc4697d72012-11-16 12:03:07 -0300376 spin_lock(&common->irqlock);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300377 /* Get the next buffer from buffer queue */
378 common->next_frm = list_entry(common->dma_queue.next,
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300379 struct vpif_disp_buffer, list);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300380 /* Remove that buffer from the buffer queue */
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300381 list_del(&common->next_frm->list);
Hans Verkuilc4697d72012-11-16 12:03:07 -0300382 spin_unlock(&common->irqlock);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300383 /* Mark status of the buffer as active */
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300384 common->next_frm->vb.state = VB2_BUF_STATE_ACTIVE;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300385
386 /* Set top and bottom field addrs in VPIF registers */
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300387 addr = vb2_dma_contig_plane_dma_addr(&common->next_frm->vb, 0);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300388 common->set_addr(addr + common->ytop_off,
389 addr + common->ybtm_off,
390 addr + common->ctop_off,
391 addr + common->cbtm_off);
392}
393
394static void process_interlaced_mode(int fid, struct common_obj *common)
395{
396 /* device field id and local field id are in sync */
397 /* If this is even field */
398 if (0 == fid) {
399 if (common->cur_frm == common->next_frm)
400 return;
401
402 /* one frame is displayed If next frame is
403 * available, release cur_frm and move on */
404 /* Copy frame display time */
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300405 do_gettimeofday(&common->cur_frm->vb.v4l2_buf.timestamp);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300406 /* Change status of the cur_frm */
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300407 vb2_buffer_done(&common->cur_frm->vb,
408 VB2_BUF_STATE_DONE);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300409 /* Make cur_frm pointing to next_frm */
410 common->cur_frm = common->next_frm;
411
412 } else if (1 == fid) { /* odd field */
Hans Verkuilc4697d72012-11-16 12:03:07 -0300413 spin_lock(&common->irqlock);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300414 if (list_empty(&common->dma_queue)
415 || (common->cur_frm != common->next_frm)) {
Hans Verkuilc4697d72012-11-16 12:03:07 -0300416 spin_unlock(&common->irqlock);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300417 return;
418 }
Hans Verkuilc4697d72012-11-16 12:03:07 -0300419 spin_unlock(&common->irqlock);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300420 /* one field is displayed configure the next
421 * frame if it is available else hold on current
422 * frame */
423 /* Get next from the buffer queue */
424 process_progressive_mode(common);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300425 }
426}
427
428/*
429 * vpif_channel_isr: It changes status of the displayed buffer, takes next
430 * buffer from the queue and sets its address in VPIF registers
431 */
432static irqreturn_t vpif_channel_isr(int irq, void *dev_id)
433{
434 struct vpif_device *dev = &vpif_obj;
435 struct channel_obj *ch;
436 struct common_obj *common;
437 enum v4l2_field field;
438 int fid = -1, i;
439 int channel_id = 0;
440
441 channel_id = *(int *)(dev_id);
Manjunath Hadlib1fc4232012-04-13 04:43:10 -0300442 if (!vpif_intr_status(channel_id + 2))
443 return IRQ_NONE;
444
Chaithrika U Se7332e32009-06-09 05:55:37 -0300445 ch = dev->dev[channel_id];
446 field = ch->common[VPIF_VIDEO_INDEX].fmt.fmt.pix.field;
447 for (i = 0; i < VPIF_NUMOBJECTS; i++) {
448 common = &ch->common[i];
449 /* If streaming is started in this channel */
450 if (0 == common->started)
451 continue;
452
453 if (1 == ch->vpifparams.std_info.frm_fmt) {
Hans Verkuilc4697d72012-11-16 12:03:07 -0300454 spin_lock(&common->irqlock);
455 if (list_empty(&common->dma_queue)) {
456 spin_unlock(&common->irqlock);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300457 continue;
Hans Verkuilc4697d72012-11-16 12:03:07 -0300458 }
459 spin_unlock(&common->irqlock);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300460
461 /* Progressive mode */
462 if (!channel_first_int[i][channel_id]) {
463 /* Mark status of the cur_frm to
464 * done and unlock semaphore on it */
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300465 do_gettimeofday(&common->cur_frm->vb.
466 v4l2_buf.timestamp);
467 vb2_buffer_done(&common->cur_frm->vb,
468 VB2_BUF_STATE_DONE);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300469 /* Make cur_frm pointing to next_frm */
470 common->cur_frm = common->next_frm;
471 }
472
473 channel_first_int[i][channel_id] = 0;
474 process_progressive_mode(common);
475 } else {
476 /* Interlaced mode */
477 /* If it is first interrupt, ignore it */
478
479 if (channel_first_int[i][channel_id]) {
480 channel_first_int[i][channel_id] = 0;
481 continue;
482 }
483
484 if (0 == i) {
485 ch->field_id ^= 1;
486 /* Get field id from VPIF registers */
487 fid = vpif_channel_getfid(ch->channel_id + 2);
488 /* If fid does not match with stored field id */
489 if (fid != ch->field_id) {
490 /* Make them in sync */
491 if (0 == fid)
492 ch->field_id = fid;
493
494 return IRQ_HANDLED;
495 }
496 }
497 process_interlaced_mode(fid, common);
498 }
499 }
500
501 return IRQ_HANDLED;
502}
503
Mats Randgaardc027e162010-12-16 12:17:44 -0300504static int vpif_update_std_info(struct channel_obj *ch)
Chaithrika U Se7332e32009-06-09 05:55:37 -0300505{
Chaithrika U Se7332e32009-06-09 05:55:37 -0300506 struct video_obj *vid_ch = &ch->video;
507 struct vpif_params *vpifparams = &ch->vpifparams;
508 struct vpif_channel_config_params *std_info = &vpifparams->std_info;
509 const struct vpif_channel_config_params *config;
510
Mats Randgaardc027e162010-12-16 12:17:44 -0300511 int i;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300512
Mats Randgaardc027e162010-12-16 12:17:44 -0300513 for (i = 0; i < vpif_ch_params_count; i++) {
514 config = &ch_params[i];
Mats Randgaard40c8bce2010-12-16 12:17:43 -0300515 if (config->hd_sd == 0) {
516 vpif_dbg(2, debug, "SD format\n");
517 if (config->stdid & vid_ch->stdid) {
518 memcpy(std_info, config, sizeof(*config));
519 break;
520 }
Chaithrika U Se7332e32009-06-09 05:55:37 -0300521 }
522 }
523
Mats Randgaardc027e162010-12-16 12:17:44 -0300524 if (i == vpif_ch_params_count) {
525 vpif_dbg(1, debug, "Format not found\n");
Mats Randgaardaa444402010-12-16 12:17:42 -0300526 return -EINVAL;
Mats Randgaardc027e162010-12-16 12:17:44 -0300527 }
528
529 return 0;
530}
531
532static int vpif_update_resolution(struct channel_obj *ch)
533{
534 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
535 struct video_obj *vid_ch = &ch->video;
536 struct vpif_params *vpifparams = &ch->vpifparams;
537 struct vpif_channel_config_params *std_info = &vpifparams->std_info;
538
Hans Verkuil0598c172012-09-18 07:18:47 -0300539 if (!vid_ch->stdid && !vid_ch->dv_timings.bt.height)
Mats Randgaardc027e162010-12-16 12:17:44 -0300540 return -EINVAL;
541
Hans Verkuil0598c172012-09-18 07:18:47 -0300542 if (vid_ch->stdid) {
Mats Randgaardc027e162010-12-16 12:17:44 -0300543 if (vpif_update_std_info(ch))
544 return -EINVAL;
545 }
Chaithrika U Se7332e32009-06-09 05:55:37 -0300546
547 common->fmt.fmt.pix.width = std_info->width;
548 common->fmt.fmt.pix.height = std_info->height;
549 vpif_dbg(1, debug, "Pixel details: Width = %d,Height = %d\n",
550 common->fmt.fmt.pix.width, common->fmt.fmt.pix.height);
551
552 /* Set height and width paramateres */
Mats Randgaardc027e162010-12-16 12:17:44 -0300553 common->height = std_info->height;
554 common->width = std_info->width;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300555
556 return 0;
557}
558
559/*
560 * vpif_calculate_offsets: This function calculates buffers offset for Y and C
561 * in the top and bottom field
562 */
563static void vpif_calculate_offsets(struct channel_obj *ch)
564{
565 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
566 struct vpif_params *vpifparams = &ch->vpifparams;
567 enum v4l2_field field = common->fmt.fmt.pix.field;
568 struct video_obj *vid_ch = &ch->video;
569 unsigned int hpitch, vpitch, sizeimage;
570
571 if (V4L2_FIELD_ANY == common->fmt.fmt.pix.field) {
572 if (ch->vpifparams.std_info.frm_fmt)
573 vid_ch->buf_field = V4L2_FIELD_NONE;
574 else
575 vid_ch->buf_field = V4L2_FIELD_INTERLACED;
576 } else {
577 vid_ch->buf_field = common->fmt.fmt.pix.field;
578 }
579
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300580 sizeimage = common->fmt.fmt.pix.sizeimage;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300581
582 hpitch = common->fmt.fmt.pix.bytesperline;
583 vpitch = sizeimage / (hpitch * 2);
584 if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
585 (V4L2_FIELD_INTERLACED == vid_ch->buf_field)) {
586 common->ytop_off = 0;
587 common->ybtm_off = hpitch;
588 common->ctop_off = sizeimage / 2;
589 common->cbtm_off = sizeimage / 2 + hpitch;
590 } else if (V4L2_FIELD_SEQ_TB == vid_ch->buf_field) {
591 common->ytop_off = 0;
592 common->ybtm_off = sizeimage / 4;
593 common->ctop_off = sizeimage / 2;
594 common->cbtm_off = common->ctop_off + sizeimage / 4;
595 } else if (V4L2_FIELD_SEQ_BT == vid_ch->buf_field) {
596 common->ybtm_off = 0;
597 common->ytop_off = sizeimage / 4;
598 common->cbtm_off = sizeimage / 2;
599 common->ctop_off = common->cbtm_off + sizeimage / 4;
600 }
601
602 if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
603 (V4L2_FIELD_INTERLACED == vid_ch->buf_field)) {
604 vpifparams->video_params.storage_mode = 1;
605 } else {
606 vpifparams->video_params.storage_mode = 0;
607 }
608
609 if (ch->vpifparams.std_info.frm_fmt == 1) {
610 vpifparams->video_params.hpitch =
611 common->fmt.fmt.pix.bytesperline;
612 } else {
613 if ((field == V4L2_FIELD_ANY) ||
614 (field == V4L2_FIELD_INTERLACED))
615 vpifparams->video_params.hpitch =
616 common->fmt.fmt.pix.bytesperline * 2;
617 else
618 vpifparams->video_params.hpitch =
619 common->fmt.fmt.pix.bytesperline;
620 }
621
622 ch->vpifparams.video_params.stdid = ch->vpifparams.std_info.stdid;
623}
624
625static void vpif_config_format(struct channel_obj *ch)
626{
627 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
628
629 common->fmt.fmt.pix.field = V4L2_FIELD_ANY;
630 if (config_params.numbuffers[ch->channel_id] == 0)
631 common->memory = V4L2_MEMORY_USERPTR;
632 else
633 common->memory = V4L2_MEMORY_MMAP;
634
635 common->fmt.fmt.pix.sizeimage =
636 config_params.channel_bufsize[ch->channel_id];
637 common->fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUV422P;
638 common->fmt.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
639}
640
641static int vpif_check_format(struct channel_obj *ch,
642 struct v4l2_pix_format *pixfmt)
643{
644 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
645 enum v4l2_field field = pixfmt->field;
646 u32 sizeimage, hpitch, vpitch;
647
648 if (pixfmt->pixelformat != V4L2_PIX_FMT_YUV422P)
649 goto invalid_fmt_exit;
650
651 if (!(VPIF_VALID_FIELD(field)))
652 goto invalid_fmt_exit;
653
654 if (pixfmt->bytesperline <= 0)
655 goto invalid_pitch_exit;
656
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300657 sizeimage = pixfmt->sizeimage;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300658
Mats Randgaardc027e162010-12-16 12:17:44 -0300659 if (vpif_update_resolution(ch))
Chaithrika U Se7332e32009-06-09 05:55:37 -0300660 return -EINVAL;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300661
662 hpitch = pixfmt->bytesperline;
663 vpitch = sizeimage / (hpitch * 2);
664
665 /* Check for valid value of pitch */
666 if ((hpitch < ch->vpifparams.std_info.width) ||
667 (vpitch < ch->vpifparams.std_info.height))
668 goto invalid_pitch_exit;
669
670 /* Check for 8 byte alignment */
671 if (!ISALIGNED(hpitch)) {
672 vpif_err("invalid pitch alignment\n");
673 return -EINVAL;
674 }
675 pixfmt->width = common->fmt.fmt.pix.width;
676 pixfmt->height = common->fmt.fmt.pix.height;
677
678 return 0;
679
680invalid_fmt_exit:
681 vpif_err("invalid field format\n");
682 return -EINVAL;
683
684invalid_pitch_exit:
685 vpif_err("invalid pitch\n");
686 return -EINVAL;
687}
688
689static void vpif_config_addr(struct channel_obj *ch, int muxmode)
690{
691 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
692
693 if (VPIF_CHANNEL3_VIDEO == ch->channel_id) {
694 common->set_addr = ch3_set_videobuf_addr;
695 } else {
696 if (2 == muxmode)
697 common->set_addr = ch2_set_videobuf_addr_yc_nmux;
698 else
699 common->set_addr = ch2_set_videobuf_addr;
700 }
701}
702
703/*
704 * vpif_mmap: It is used to map kernel space buffers into user spaces
705 */
706static int vpif_mmap(struct file *filep, struct vm_area_struct *vma)
707{
708 struct vpif_fh *fh = filep->private_data;
Mats Randgaard2c0ddd12010-12-16 12:17:45 -0300709 struct channel_obj *ch = fh->channel;
710 struct common_obj *common = &(ch->common[VPIF_VIDEO_INDEX]);
Hans Verkuil0b7286d2012-07-31 03:47:07 -0300711 int ret;
Mats Randgaard2c0ddd12010-12-16 12:17:45 -0300712
713 vpif_dbg(2, debug, "vpif_mmap\n");
Chaithrika U Se7332e32009-06-09 05:55:37 -0300714
Hans Verkuil0b7286d2012-07-31 03:47:07 -0300715 if (mutex_lock_interruptible(&common->lock))
716 return -ERESTARTSYS;
717 ret = vb2_mmap(&common->buffer_queue, vma);
718 mutex_unlock(&common->lock);
719 return ret;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300720}
721
722/*
723 * vpif_poll: It is used for select/poll system call
724 */
725static unsigned int vpif_poll(struct file *filep, poll_table *wait)
726{
727 struct vpif_fh *fh = filep->private_data;
728 struct channel_obj *ch = fh->channel;
729 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
Hans Verkuil0b7286d2012-07-31 03:47:07 -0300730 unsigned int res = 0;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300731
Hans Verkuil0b7286d2012-07-31 03:47:07 -0300732 if (common->started) {
733 mutex_lock(&common->lock);
734 res = vb2_poll(&common->buffer_queue, filep, wait);
735 mutex_unlock(&common->lock);
736 }
Chaithrika U Se7332e32009-06-09 05:55:37 -0300737
Hans Verkuil0b7286d2012-07-31 03:47:07 -0300738 return res;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300739}
740
741/*
742 * vpif_open: It creates object of file handle structure and stores it in
743 * private_data member of filepointer
744 */
745static int vpif_open(struct file *filep)
746{
747 struct video_device *vdev = video_devdata(filep);
Hans Verkuil0b7286d2012-07-31 03:47:07 -0300748 struct channel_obj *ch = video_get_drvdata(vdev);
749 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
750 struct vpif_fh *fh;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300751
Chaithrika U Se7332e32009-06-09 05:55:37 -0300752 /* Allocate memory for the file handle object */
Mats Randgaard1f8766b2010-08-30 10:30:37 -0300753 fh = kzalloc(sizeof(struct vpif_fh), GFP_KERNEL);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300754 if (fh == NULL) {
755 vpif_err("unable to allocate memory for file handle object\n");
756 return -ENOMEM;
757 }
758
Hans Verkuil0b7286d2012-07-31 03:47:07 -0300759 if (mutex_lock_interruptible(&common->lock)) {
760 kfree(fh);
761 return -ERESTARTSYS;
762 }
Chaithrika U Se7332e32009-06-09 05:55:37 -0300763 /* store pointer to fh in private_data member of filep */
764 filep->private_data = fh;
765 fh->channel = ch;
766 fh->initialized = 0;
767 if (!ch->initialized) {
768 fh->initialized = 1;
769 ch->initialized = 1;
770 memset(&ch->vpifparams, 0, sizeof(ch->vpifparams));
771 }
772
773 /* Increment channel usrs counter */
774 atomic_inc(&ch->usrs);
775 /* Set io_allowed[VPIF_VIDEO_INDEX] member to false */
776 fh->io_allowed[VPIF_VIDEO_INDEX] = 0;
777 /* Initialize priority of this instance to default priority */
778 fh->prio = V4L2_PRIORITY_UNSET;
779 v4l2_prio_open(&ch->prio, &fh->prio);
Hans Verkuil0b7286d2012-07-31 03:47:07 -0300780 mutex_unlock(&common->lock);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300781
782 return 0;
783}
784
785/*
786 * vpif_release: This function deletes buffer queue, frees the buffers and
787 * the vpif file handle
788 */
789static int vpif_release(struct file *filep)
790{
791 struct vpif_fh *fh = filep->private_data;
792 struct channel_obj *ch = fh->channel;
793 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
794
Hans Verkuil0b7286d2012-07-31 03:47:07 -0300795 mutex_lock(&common->lock);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300796 /* if this instance is doing IO */
797 if (fh->io_allowed[VPIF_VIDEO_INDEX]) {
798 /* Reset io_usrs member of channel object */
799 common->io_usrs = 0;
800 /* Disable channel */
801 if (VPIF_CHANNEL2_VIDEO == ch->channel_id) {
802 enable_channel2(0);
803 channel2_intr_enable(0);
804 }
805 if ((VPIF_CHANNEL3_VIDEO == ch->channel_id) ||
806 (2 == common->started)) {
807 enable_channel3(0);
808 channel3_intr_enable(0);
809 }
810 common->started = 0;
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300811
Chaithrika U Se7332e32009-06-09 05:55:37 -0300812 /* Free buffers allocated */
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300813 vb2_queue_release(&common->buffer_queue);
814 vb2_dma_contig_cleanup_ctx(common->alloc_ctx);
815
Chaithrika U Se7332e32009-06-09 05:55:37 -0300816 common->numbuffers =
817 config_params.numbuffers[ch->channel_id];
818 }
819
Chaithrika U Se7332e32009-06-09 05:55:37 -0300820 /* Decrement channel usrs counter */
821 atomic_dec(&ch->usrs);
822 /* If this file handle has initialize encoder device, reset it */
823 if (fh->initialized)
824 ch->initialized = 0;
825
826 /* Close the priority */
Hans Verkuilffb48772010-05-01 08:03:24 -0300827 v4l2_prio_close(&ch->prio, fh->prio);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300828 filep->private_data = NULL;
829 fh->initialized = 0;
Hans Verkuil0b7286d2012-07-31 03:47:07 -0300830 mutex_unlock(&common->lock);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300831 kfree(fh);
832
833 return 0;
834}
835
836/* functions implementing ioctls */
Mats Randgaard2c0ddd12010-12-16 12:17:45 -0300837/**
838 * vpif_querycap() - QUERYCAP handler
839 * @file: file ptr
840 * @priv: file handle
841 * @cap: ptr to v4l2_capability structure
842 */
Chaithrika U Se7332e32009-06-09 05:55:37 -0300843static int vpif_querycap(struct file *file, void *priv,
844 struct v4l2_capability *cap)
845{
Muralidharan Karicheri317b2e22009-09-16 14:30:53 -0300846 struct vpif_display_config *config = vpif_dev->platform_data;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300847
Lad, Prabhakar626d533f2012-09-25 11:21:55 -0300848 cap->device_caps = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING;
849 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
850 snprintf(cap->driver, sizeof(cap->driver), "%s", dev_name(vpif_dev));
851 snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
852 dev_name(vpif_dev));
Chaithrika U Se7332e32009-06-09 05:55:37 -0300853 strlcpy(cap->card, config->card_name, sizeof(cap->card));
854
855 return 0;
856}
857
858static int vpif_enum_fmt_vid_out(struct file *file, void *priv,
859 struct v4l2_fmtdesc *fmt)
860{
861 if (fmt->index != 0) {
862 vpif_err("Invalid format index\n");
863 return -EINVAL;
864 }
865
866 /* Fill in the information about format */
867 fmt->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
868 strcpy(fmt->description, "YCbCr4:2:2 YC Planar");
869 fmt->pixelformat = V4L2_PIX_FMT_YUV422P;
870
871 return 0;
872}
873
874static int vpif_g_fmt_vid_out(struct file *file, void *priv,
875 struct v4l2_format *fmt)
876{
877 struct vpif_fh *fh = priv;
878 struct channel_obj *ch = fh->channel;
879 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
880
881 /* Check the validity of the buffer type */
882 if (common->fmt.type != fmt->type)
883 return -EINVAL;
884
Mats Randgaardc027e162010-12-16 12:17:44 -0300885 if (vpif_update_resolution(ch))
Hans Verkuil9bfaae22011-01-05 13:35:45 -0300886 return -EINVAL;
887 *fmt = common->fmt;
888 return 0;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300889}
890
891static int vpif_s_fmt_vid_out(struct file *file, void *priv,
892 struct v4l2_format *fmt)
893{
894 struct vpif_fh *fh = priv;
895 struct v4l2_pix_format *pixfmt;
896 struct channel_obj *ch = fh->channel;
897 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
898 int ret = 0;
899
900 if ((VPIF_CHANNEL2_VIDEO == ch->channel_id)
901 || (VPIF_CHANNEL3_VIDEO == ch->channel_id)) {
902 if (!fh->initialized) {
903 vpif_dbg(1, debug, "Channel Busy\n");
904 return -EBUSY;
905 }
906
907 /* Check for the priority */
Hans Verkuilffb48772010-05-01 08:03:24 -0300908 ret = v4l2_prio_check(&ch->prio, fh->prio);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300909 if (0 != ret)
910 return ret;
911 fh->initialized = 1;
912 }
913
914 if (common->started) {
915 vpif_dbg(1, debug, "Streaming in progress\n");
916 return -EBUSY;
917 }
918
919 pixfmt = &fmt->fmt.pix;
920 /* Check for valid field format */
921 ret = vpif_check_format(ch, pixfmt);
922 if (ret)
923 return ret;
924
925 /* store the pix format in the channel object */
926 common->fmt.fmt.pix = *pixfmt;
927 /* store the format in the channel object */
Chaithrika U Se7332e32009-06-09 05:55:37 -0300928 common->fmt = *fmt;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300929 return 0;
930}
931
932static int vpif_try_fmt_vid_out(struct file *file, void *priv,
933 struct v4l2_format *fmt)
934{
935 struct vpif_fh *fh = priv;
936 struct channel_obj *ch = fh->channel;
937 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
938 struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
939 int ret = 0;
940
941 ret = vpif_check_format(ch, pixfmt);
942 if (ret) {
943 *pixfmt = common->fmt.fmt.pix;
944 pixfmt->sizeimage = pixfmt->width * pixfmt->height * 2;
945 }
946
947 return ret;
948}
949
950static int vpif_reqbufs(struct file *file, void *priv,
951 struct v4l2_requestbuffers *reqbuf)
952{
953 struct vpif_fh *fh = priv;
954 struct channel_obj *ch = fh->channel;
955 struct common_obj *common;
956 enum v4l2_field field;
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300957 struct vb2_queue *q;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300958 u8 index = 0;
Lad, Prabhakarb4a711e72012-10-03 10:01:07 -0300959 int ret;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300960
961 /* This file handle has not initialized the channel,
962 It is not allowed to do settings */
963 if ((VPIF_CHANNEL2_VIDEO == ch->channel_id)
964 || (VPIF_CHANNEL3_VIDEO == ch->channel_id)) {
965 if (!fh->initialized) {
966 vpif_err("Channel Busy\n");
967 return -EBUSY;
968 }
969 }
970
971 if (V4L2_BUF_TYPE_VIDEO_OUTPUT != reqbuf->type)
972 return -EINVAL;
973
974 index = VPIF_VIDEO_INDEX;
975
976 common = &ch->common[index];
Chaithrika U S13df4f62009-06-22 09:02:55 -0300977
Manjunath Hadlifc613d42012-04-13 04:49:10 -0300978 if (common->fmt.type != reqbuf->type || !vpif_dev)
Hans Verkuil9bfaae22011-01-05 13:35:45 -0300979 return -EINVAL;
Hans Verkuil9bfaae22011-01-05 13:35:45 -0300980 if (0 != common->io_usrs)
981 return -EBUSY;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300982
983 if (reqbuf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
984 if (common->fmt.fmt.pix.field == V4L2_FIELD_ANY)
985 field = V4L2_FIELD_INTERLACED;
986 else
987 field = common->fmt.fmt.pix.field;
988 } else {
989 field = V4L2_VBI_INTERLACED;
990 }
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300991 /* Initialize videobuf2 queue as per the buffer type */
992 common->alloc_ctx = vb2_dma_contig_init_ctx(vpif_dev);
Wei Yongjun94b76a82012-11-15 09:18:17 -0300993 if (IS_ERR(common->alloc_ctx)) {
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300994 vpif_err("Failed to get the context\n");
Wei Yongjun94b76a82012-11-15 09:18:17 -0300995 return PTR_ERR(common->alloc_ctx);
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300996 }
997 q = &common->buffer_queue;
998 q->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
999 q->io_modes = VB2_MMAP | VB2_USERPTR;
1000 q->drv_priv = fh;
1001 q->ops = &video_qops;
1002 q->mem_ops = &vb2_dma_contig_memops;
1003 q->buf_struct_size = sizeof(struct vpif_disp_buffer);
Chaithrika U Se7332e32009-06-09 05:55:37 -03001004
Lad, Prabhakarb4a711e72012-10-03 10:01:07 -03001005 ret = vb2_queue_init(q);
1006 if (ret) {
1007 vpif_err("vpif_display: vb2_queue_init() failed\n");
1008 vb2_dma_contig_cleanup_ctx(common->alloc_ctx);
1009 return ret;
1010 }
Chaithrika U Se7332e32009-06-09 05:55:37 -03001011 /* Set io allowed member of file handle to TRUE */
1012 fh->io_allowed[index] = 1;
1013 /* Increment io usrs member of channel object to 1 */
1014 common->io_usrs = 1;
1015 /* Store type of memory requested in channel object */
1016 common->memory = reqbuf->memory;
1017 INIT_LIST_HEAD(&common->dma_queue);
Chaithrika U Se7332e32009-06-09 05:55:37 -03001018 /* Allocate buffers */
Lad, Prabhakar2401dd22012-06-28 09:28:36 -03001019 return vb2_reqbufs(&common->buffer_queue, reqbuf);
Chaithrika U Se7332e32009-06-09 05:55:37 -03001020}
1021
1022static int vpif_querybuf(struct file *file, void *priv,
1023 struct v4l2_buffer *tbuf)
1024{
1025 struct vpif_fh *fh = priv;
1026 struct channel_obj *ch = fh->channel;
1027 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1028
1029 if (common->fmt.type != tbuf->type)
1030 return -EINVAL;
1031
Lad, Prabhakar2401dd22012-06-28 09:28:36 -03001032 return vb2_querybuf(&common->buffer_queue, tbuf);
Chaithrika U Se7332e32009-06-09 05:55:37 -03001033}
1034
1035static int vpif_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
1036{
Lad, Prabhakar2401dd22012-06-28 09:28:36 -03001037 struct vpif_fh *fh = NULL;
1038 struct channel_obj *ch = NULL;
1039 struct common_obj *common = NULL;
Chaithrika U Se7332e32009-06-09 05:55:37 -03001040
Lad, Prabhakar2401dd22012-06-28 09:28:36 -03001041 if (!buf || !priv)
1042 return -EINVAL;
Chaithrika U Se7332e32009-06-09 05:55:37 -03001043
Lad, Prabhakar2401dd22012-06-28 09:28:36 -03001044 fh = priv;
1045 ch = fh->channel;
1046 if (!ch)
1047 return -EINVAL;
1048
1049 common = &(ch->common[VPIF_VIDEO_INDEX]);
1050 if (common->fmt.type != buf->type)
Chaithrika U Se7332e32009-06-09 05:55:37 -03001051 return -EINVAL;
1052
1053 if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
1054 vpif_err("fh->io_allowed\n");
1055 return -EACCES;
1056 }
1057
Lad, Prabhakar2401dd22012-06-28 09:28:36 -03001058 return vb2_qbuf(&common->buffer_queue, buf);
Chaithrika U Se7332e32009-06-09 05:55:37 -03001059}
1060
1061static int vpif_s_std(struct file *file, void *priv, v4l2_std_id *std_id)
1062{
1063 struct vpif_fh *fh = priv;
1064 struct channel_obj *ch = fh->channel;
1065 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1066 int ret = 0;
1067
Manjunath Hadli0a631722012-04-13 04:44:00 -03001068 if (!(*std_id & VPIF_V4L2_STD))
Chaithrika U Se7332e32009-06-09 05:55:37 -03001069 return -EINVAL;
1070
1071 if (common->started) {
1072 vpif_err("streaming in progress\n");
1073 return -EBUSY;
1074 }
1075
1076 /* Call encoder subdevice function to set the standard */
Chaithrika U Se7332e32009-06-09 05:55:37 -03001077 ch->video.stdid = *std_id;
Hans Verkuil0598c172012-09-18 07:18:47 -03001078 memset(&ch->video.dv_timings, 0, sizeof(ch->video.dv_timings));
Chaithrika U Se7332e32009-06-09 05:55:37 -03001079 /* Get the information about the standard */
Hans Verkuil9bfaae22011-01-05 13:35:45 -03001080 if (vpif_update_resolution(ch))
1081 return -EINVAL;
Chaithrika U Se7332e32009-06-09 05:55:37 -03001082
1083 if ((ch->vpifparams.std_info.width *
1084 ch->vpifparams.std_info.height * 2) >
1085 config_params.channel_bufsize[ch->channel_id]) {
1086 vpif_err("invalid std for this size\n");
Hans Verkuil9bfaae22011-01-05 13:35:45 -03001087 return -EINVAL;
Chaithrika U Se7332e32009-06-09 05:55:37 -03001088 }
1089
1090 common->fmt.fmt.pix.bytesperline = common->fmt.fmt.pix.width;
1091 /* Configure the default format information */
1092 vpif_config_format(ch);
1093
1094 ret = v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 1, video,
1095 s_std_output, *std_id);
1096 if (ret < 0) {
1097 vpif_err("Failed to set output standard\n");
Hans Verkuil9bfaae22011-01-05 13:35:45 -03001098 return ret;
Chaithrika U Se7332e32009-06-09 05:55:37 -03001099 }
1100
1101 ret = v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 1, core,
1102 s_std, *std_id);
1103 if (ret < 0)
1104 vpif_err("Failed to set standard for sub devices\n");
Chaithrika U Se7332e32009-06-09 05:55:37 -03001105 return ret;
1106}
1107
1108static int vpif_g_std(struct file *file, void *priv, v4l2_std_id *std)
1109{
1110 struct vpif_fh *fh = priv;
1111 struct channel_obj *ch = fh->channel;
1112
1113 *std = ch->video.stdid;
1114 return 0;
1115}
1116
1117static int vpif_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
1118{
1119 struct vpif_fh *fh = priv;
1120 struct channel_obj *ch = fh->channel;
1121 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1122
Lad, Prabhakar2401dd22012-06-28 09:28:36 -03001123 return vb2_dqbuf(&common->buffer_queue, p,
Chaithrika U Se7332e32009-06-09 05:55:37 -03001124 (file->f_flags & O_NONBLOCK));
1125}
1126
1127static int vpif_streamon(struct file *file, void *priv,
1128 enum v4l2_buf_type buftype)
1129{
1130 struct vpif_fh *fh = priv;
1131 struct channel_obj *ch = fh->channel;
1132 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1133 struct channel_obj *oth_ch = vpif_obj.dev[!ch->channel_id];
Chaithrika U Se7332e32009-06-09 05:55:37 -03001134 int ret = 0;
1135
1136 if (buftype != V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1137 vpif_err("buffer type not supported\n");
1138 return -EINVAL;
1139 }
1140
1141 if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
1142 vpif_err("fh->io_allowed\n");
1143 return -EACCES;
1144 }
1145
1146 /* If Streaming is already started, return error */
1147 if (common->started) {
1148 vpif_err("channel->started\n");
1149 return -EBUSY;
1150 }
1151
1152 if ((ch->channel_id == VPIF_CHANNEL2_VIDEO
1153 && oth_ch->common[VPIF_VIDEO_INDEX].started &&
1154 ch->vpifparams.std_info.ycmux_mode == 0)
1155 || ((ch->channel_id == VPIF_CHANNEL3_VIDEO)
1156 && (2 == oth_ch->common[VPIF_VIDEO_INDEX].started))) {
1157 vpif_err("other channel is using\n");
1158 return -EBUSY;
1159 }
1160
1161 ret = vpif_check_format(ch, &common->fmt.fmt.pix);
1162 if (ret < 0)
1163 return ret;
1164
Lad, Prabhakar2401dd22012-06-28 09:28:36 -03001165 /* Call vb2_streamon to start streaming in videobuf2 */
1166 ret = vb2_streamon(&common->buffer_queue, buftype);
Chaithrika U Se7332e32009-06-09 05:55:37 -03001167 if (ret < 0) {
Lad, Prabhakar2401dd22012-06-28 09:28:36 -03001168 vpif_err("vb2_streamon\n");
Chaithrika U Se7332e32009-06-09 05:55:37 -03001169 return ret;
1170 }
1171
Chaithrika U Se7332e32009-06-09 05:55:37 -03001172 return ret;
1173}
1174
1175static int vpif_streamoff(struct file *file, void *priv,
1176 enum v4l2_buf_type buftype)
1177{
1178 struct vpif_fh *fh = priv;
1179 struct channel_obj *ch = fh->channel;
1180 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
Manjunath Hadli6964b102012-06-29 03:20:12 -03001181 struct vpif_display_config *vpif_config_data =
1182 vpif_dev->platform_data;
Chaithrika U Se7332e32009-06-09 05:55:37 -03001183
1184 if (buftype != V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1185 vpif_err("buffer type not supported\n");
1186 return -EINVAL;
1187 }
1188
1189 if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
1190 vpif_err("fh->io_allowed\n");
1191 return -EACCES;
1192 }
1193
1194 if (!common->started) {
1195 vpif_err("channel->started\n");
1196 return -EINVAL;
1197 }
1198
Chaithrika U Se7332e32009-06-09 05:55:37 -03001199 if (buftype == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1200 /* disable channel */
1201 if (VPIF_CHANNEL2_VIDEO == ch->channel_id) {
Lad, Prabhakar2bd4e582012-09-25 08:11:49 -03001202 if (vpif_config_data->
1203 chan_config[VPIF_CHANNEL2_VIDEO].clip_en)
Manjunath Hadli6964b102012-06-29 03:20:12 -03001204 channel2_clipping_enable(0);
Chaithrika U Se7332e32009-06-09 05:55:37 -03001205 enable_channel2(0);
1206 channel2_intr_enable(0);
1207 }
1208 if ((VPIF_CHANNEL3_VIDEO == ch->channel_id) ||
1209 (2 == common->started)) {
Lad, Prabhakar2bd4e582012-09-25 08:11:49 -03001210 if (vpif_config_data->
1211 chan_config[VPIF_CHANNEL3_VIDEO].clip_en)
Manjunath Hadli6964b102012-06-29 03:20:12 -03001212 channel3_clipping_enable(0);
Chaithrika U Se7332e32009-06-09 05:55:37 -03001213 enable_channel3(0);
1214 channel3_intr_enable(0);
1215 }
1216 }
1217
1218 common->started = 0;
Lad, Prabhakar2401dd22012-06-28 09:28:36 -03001219 return vb2_streamoff(&common->buffer_queue, buftype);
Chaithrika U Se7332e32009-06-09 05:55:37 -03001220}
1221
1222static int vpif_cropcap(struct file *file, void *priv,
1223 struct v4l2_cropcap *crop)
1224{
1225 struct vpif_fh *fh = priv;
1226 struct channel_obj *ch = fh->channel;
1227 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1228 if (V4L2_BUF_TYPE_VIDEO_OUTPUT != crop->type)
1229 return -EINVAL;
1230
1231 crop->bounds.left = crop->bounds.top = 0;
1232 crop->defrect.left = crop->defrect.top = 0;
1233 crop->defrect.height = crop->bounds.height = common->height;
1234 crop->defrect.width = crop->bounds.width = common->width;
1235
1236 return 0;
1237}
1238
1239static int vpif_enum_output(struct file *file, void *fh,
1240 struct v4l2_output *output)
1241{
1242
Muralidharan Karicheri317b2e22009-09-16 14:30:53 -03001243 struct vpif_display_config *config = vpif_dev->platform_data;
Lad, Prabhakar2bd4e582012-09-25 08:11:49 -03001244 struct vpif_display_chan_config *chan_cfg;
1245 struct vpif_fh *vpif_handler = fh;
1246 struct channel_obj *ch = vpif_handler->channel;
Chaithrika U Se7332e32009-06-09 05:55:37 -03001247
Lad, Prabhakar2bd4e582012-09-25 08:11:49 -03001248 chan_cfg = &config->chan_config[ch->channel_id];
1249 if (output->index >= chan_cfg->output_count) {
Chaithrika U Se7332e32009-06-09 05:55:37 -03001250 vpif_dbg(1, debug, "Invalid output index\n");
1251 return -EINVAL;
1252 }
1253
Lad, Prabhakar2bd4e582012-09-25 08:11:49 -03001254 *output = chan_cfg->outputs[output->index].output;
1255 return 0;
1256}
Chaithrika U Se7332e32009-06-09 05:55:37 -03001257
Lad, Prabhakar2bd4e582012-09-25 08:11:49 -03001258/**
1259 * vpif_output_to_subdev() - Maps output to sub device
1260 * @vpif_cfg - global config ptr
1261 * @chan_cfg - channel config ptr
1262 * @index - Given output index from application
1263 *
1264 * lookup the sub device information for a given output index.
1265 * we report all the output to application. output table also
1266 * has sub device name for the each output
1267 */
1268static int
1269vpif_output_to_subdev(struct vpif_display_config *vpif_cfg,
1270 struct vpif_display_chan_config *chan_cfg, int index)
1271{
1272 struct vpif_subdev_info *subdev_info;
1273 const char *subdev_name;
1274 int i;
1275
1276 vpif_dbg(2, debug, "vpif_output_to_subdev\n");
1277
1278 if (chan_cfg->outputs == NULL)
1279 return -1;
1280
1281 subdev_name = chan_cfg->outputs[index].subdev_name;
1282 if (subdev_name == NULL)
1283 return -1;
1284
1285 /* loop through the sub device list to get the sub device info */
1286 for (i = 0; i < vpif_cfg->subdev_count; i++) {
1287 subdev_info = &vpif_cfg->subdevinfo[i];
1288 if (!strcmp(subdev_info->name, subdev_name))
1289 return i;
1290 }
1291 return -1;
1292}
1293
1294/**
1295 * vpif_set_output() - Select an output
1296 * @vpif_cfg - global config ptr
1297 * @ch - channel
1298 * @index - Given output index from application
1299 *
1300 * Select the given output.
1301 */
1302static int vpif_set_output(struct vpif_display_config *vpif_cfg,
1303 struct channel_obj *ch, int index)
1304{
1305 struct vpif_display_chan_config *chan_cfg =
1306 &vpif_cfg->chan_config[ch->channel_id];
1307 struct vpif_subdev_info *subdev_info = NULL;
1308 struct v4l2_subdev *sd = NULL;
1309 u32 input = 0, output = 0;
1310 int sd_index;
1311 int ret;
1312
1313 sd_index = vpif_output_to_subdev(vpif_cfg, chan_cfg, index);
1314 if (sd_index >= 0) {
1315 sd = vpif_obj.sd[sd_index];
1316 subdev_info = &vpif_cfg->subdevinfo[sd_index];
1317 }
1318
1319 if (sd) {
1320 input = chan_cfg->outputs[index].input_route;
1321 output = chan_cfg->outputs[index].output_route;
1322 ret = v4l2_subdev_call(sd, video, s_routing, input, output, 0);
1323 if (ret < 0 && ret != -ENOIOCTLCMD) {
1324 vpif_err("Failed to set output\n");
1325 return ret;
1326 }
1327
1328 }
1329 ch->output_idx = index;
1330 ch->sd = sd;
1331 if (chan_cfg->outputs != NULL)
1332 /* update tvnorms from the sub device output info */
1333 ch->video_dev->tvnorms = chan_cfg->outputs[index].output.std;
Chaithrika U Se7332e32009-06-09 05:55:37 -03001334 return 0;
1335}
1336
1337static int vpif_s_output(struct file *file, void *priv, unsigned int i)
1338{
Lad, Prabhakar2bd4e582012-09-25 08:11:49 -03001339 struct vpif_display_config *config = vpif_dev->platform_data;
1340 struct vpif_display_chan_config *chan_cfg;
Chaithrika U Se7332e32009-06-09 05:55:37 -03001341 struct vpif_fh *fh = priv;
1342 struct channel_obj *ch = fh->channel;
Chaithrika U Se7332e32009-06-09 05:55:37 -03001343 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
Lad, Prabhakar2bd4e582012-09-25 08:11:49 -03001344
1345 chan_cfg = &config->chan_config[ch->channel_id];
1346
1347 if (i >= chan_cfg->output_count)
1348 return -EINVAL;
Chaithrika U Se7332e32009-06-09 05:55:37 -03001349
Chaithrika U Se7332e32009-06-09 05:55:37 -03001350 if (common->started) {
1351 vpif_err("Streaming in progress\n");
Hans Verkuil9bfaae22011-01-05 13:35:45 -03001352 return -EBUSY;
Chaithrika U Se7332e32009-06-09 05:55:37 -03001353 }
1354
Lad, Prabhakar2bd4e582012-09-25 08:11:49 -03001355 return vpif_set_output(config, ch, i);
Chaithrika U Se7332e32009-06-09 05:55:37 -03001356}
1357
1358static int vpif_g_output(struct file *file, void *priv, unsigned int *i)
1359{
1360 struct vpif_fh *fh = priv;
1361 struct channel_obj *ch = fh->channel;
Chaithrika U Se7332e32009-06-09 05:55:37 -03001362
Hans Verkuil311673e2012-09-20 09:06:23 -03001363 *i = ch->output_idx;
Chaithrika U Se7332e32009-06-09 05:55:37 -03001364
1365 return 0;
1366}
1367
1368static int vpif_g_priority(struct file *file, void *priv, enum v4l2_priority *p)
1369{
1370 struct vpif_fh *fh = priv;
1371 struct channel_obj *ch = fh->channel;
1372
1373 *p = v4l2_prio_max(&ch->prio);
1374
1375 return 0;
1376}
1377
1378static int vpif_s_priority(struct file *file, void *priv, enum v4l2_priority p)
1379{
1380 struct vpif_fh *fh = priv;
1381 struct channel_obj *ch = fh->channel;
1382
1383 return v4l2_prio_change(&ch->prio, &fh->prio, p);
1384}
1385
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001386/**
Hans Verkuil0598c172012-09-18 07:18:47 -03001387 * vpif_enum_dv_timings() - ENUM_DV_TIMINGS handler
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001388 * @file: file ptr
1389 * @priv: file handle
Hans Verkuil0598c172012-09-18 07:18:47 -03001390 * @timings: input timings
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001391 */
Hans Verkuil0598c172012-09-18 07:18:47 -03001392static int
1393vpif_enum_dv_timings(struct file *file, void *priv,
1394 struct v4l2_enum_dv_timings *timings)
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001395{
1396 struct vpif_fh *fh = priv;
1397 struct channel_obj *ch = fh->channel;
Lad, Prabhakar2bd4e582012-09-25 08:11:49 -03001398 int ret;
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001399
Lad, Prabhakar2bd4e582012-09-25 08:11:49 -03001400 ret = v4l2_subdev_call(ch->sd, video, enum_dv_timings, timings);
Wei Yongjun63af4af2012-10-30 09:49:38 -03001401 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
Lad, Prabhakar2bd4e582012-09-25 08:11:49 -03001402 return -EINVAL;
1403 return ret;
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001404}
1405
1406/**
Mats Randgaardc027e162010-12-16 12:17:44 -03001407 * vpif_s_dv_timings() - S_DV_TIMINGS handler
1408 * @file: file ptr
1409 * @priv: file handle
1410 * @timings: digital video timings
1411 */
1412static int vpif_s_dv_timings(struct file *file, void *priv,
1413 struct v4l2_dv_timings *timings)
1414{
1415 struct vpif_fh *fh = priv;
1416 struct channel_obj *ch = fh->channel;
1417 struct vpif_params *vpifparams = &ch->vpifparams;
1418 struct vpif_channel_config_params *std_info = &vpifparams->std_info;
1419 struct video_obj *vid_ch = &ch->video;
Hans Verkuil0598c172012-09-18 07:18:47 -03001420 struct v4l2_bt_timings *bt = &vid_ch->dv_timings.bt;
Mats Randgaardc027e162010-12-16 12:17:44 -03001421 int ret;
1422
1423 if (timings->type != V4L2_DV_BT_656_1120) {
1424 vpif_dbg(2, debug, "Timing type not defined\n");
1425 return -EINVAL;
1426 }
1427
1428 /* Configure subdevice timings, if any */
Hans Verkuil882084a2012-09-20 09:06:31 -03001429 ret = v4l2_subdev_call(ch->sd, video, s_dv_timings, timings);
Lad, Prabhakar2bd4e582012-09-25 08:11:49 -03001430 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
1431 ret = 0;
1432 if (ret < 0) {
Mats Randgaardc027e162010-12-16 12:17:44 -03001433 vpif_dbg(2, debug, "Error setting custom DV timings\n");
1434 return ret;
1435 }
1436
1437 if (!(timings->bt.width && timings->bt.height &&
1438 (timings->bt.hbackporch ||
1439 timings->bt.hfrontporch ||
1440 timings->bt.hsync) &&
1441 timings->bt.vfrontporch &&
1442 (timings->bt.vbackporch ||
1443 timings->bt.vsync))) {
1444 vpif_dbg(2, debug, "Timings for width, height, "
1445 "horizontal back porch, horizontal sync, "
1446 "horizontal front porch, vertical back porch, "
1447 "vertical sync and vertical back porch "
1448 "must be defined\n");
1449 return -EINVAL;
1450 }
1451
Hans Verkuil0598c172012-09-18 07:18:47 -03001452 vid_ch->dv_timings = *timings;
Mats Randgaardc027e162010-12-16 12:17:44 -03001453
1454 /* Configure video port timings */
1455
1456 std_info->eav2sav = bt->hbackporch + bt->hfrontporch +
1457 bt->hsync - 8;
1458 std_info->sav2eav = bt->width;
1459
1460 std_info->l1 = 1;
1461 std_info->l3 = bt->vsync + bt->vbackporch + 1;
1462
1463 if (bt->interlaced) {
1464 if (bt->il_vbackporch || bt->il_vfrontporch || bt->il_vsync) {
1465 std_info->vsize = bt->height * 2 +
1466 bt->vfrontporch + bt->vsync + bt->vbackporch +
1467 bt->il_vfrontporch + bt->il_vsync +
1468 bt->il_vbackporch;
1469 std_info->l5 = std_info->vsize/2 -
1470 (bt->vfrontporch - 1);
1471 std_info->l7 = std_info->vsize/2 + 1;
1472 std_info->l9 = std_info->l7 + bt->il_vsync +
1473 bt->il_vbackporch + 1;
1474 std_info->l11 = std_info->vsize -
1475 (bt->il_vfrontporch - 1);
1476 } else {
1477 vpif_dbg(2, debug, "Required timing values for "
1478 "interlaced BT format missing\n");
1479 return -EINVAL;
1480 }
1481 } else {
1482 std_info->vsize = bt->height + bt->vfrontporch +
1483 bt->vsync + bt->vbackporch;
1484 std_info->l5 = std_info->vsize - (bt->vfrontporch - 1);
1485 }
1486 strncpy(std_info->name, "Custom timings BT656/1120",
1487 VPIF_MAX_NAME);
1488 std_info->width = bt->width;
1489 std_info->height = bt->height;
1490 std_info->frm_fmt = bt->interlaced ? 0 : 1;
1491 std_info->ycmux_mode = 0;
1492 std_info->capture_format = 0;
1493 std_info->vbi_supported = 0;
1494 std_info->hd_sd = 1;
1495 std_info->stdid = 0;
Mats Randgaardc027e162010-12-16 12:17:44 -03001496 vid_ch->stdid = 0;
Mats Randgaardc027e162010-12-16 12:17:44 -03001497
1498 return 0;
1499}
1500
1501/**
1502 * vpif_g_dv_timings() - G_DV_TIMINGS handler
1503 * @file: file ptr
1504 * @priv: file handle
1505 * @timings: digital video timings
1506 */
1507static int vpif_g_dv_timings(struct file *file, void *priv,
1508 struct v4l2_dv_timings *timings)
1509{
1510 struct vpif_fh *fh = priv;
1511 struct channel_obj *ch = fh->channel;
1512 struct video_obj *vid_ch = &ch->video;
Mats Randgaardc027e162010-12-16 12:17:44 -03001513
Hans Verkuil0598c172012-09-18 07:18:47 -03001514 *timings = vid_ch->dv_timings;
Mats Randgaardc027e162010-12-16 12:17:44 -03001515
1516 return 0;
1517}
Mats Randgaard7036d6a2010-12-16 12:17:41 -03001518
1519/*
1520 * vpif_g_chip_ident() - Identify the chip
1521 * @file: file ptr
1522 * @priv: file handle
1523 * @chip: chip identity
1524 *
1525 * Returns zero or -EINVAL if read operations fails.
1526 */
1527static int vpif_g_chip_ident(struct file *file, void *priv,
1528 struct v4l2_dbg_chip_ident *chip)
1529{
1530 chip->ident = V4L2_IDENT_NONE;
1531 chip->revision = 0;
1532 if (chip->match.type != V4L2_CHIP_MATCH_I2C_DRIVER &&
1533 chip->match.type != V4L2_CHIP_MATCH_I2C_ADDR) {
1534 vpif_dbg(2, debug, "match_type is invalid.\n");
1535 return -EINVAL;
1536 }
1537
1538 return v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 0, core,
1539 g_chip_ident, chip);
1540}
1541
1542#ifdef CONFIG_VIDEO_ADV_DEBUG
1543/*
1544 * vpif_dbg_g_register() - Read register
1545 * @file: file ptr
1546 * @priv: file handle
1547 * @reg: register to be read
1548 *
1549 * Debugging only
1550 * Returns zero or -EINVAL if read operations fails.
1551 */
1552static int vpif_dbg_g_register(struct file *file, void *priv,
1553 struct v4l2_dbg_register *reg){
1554 struct vpif_fh *fh = priv;
1555 struct channel_obj *ch = fh->channel;
Mats Randgaard7036d6a2010-12-16 12:17:41 -03001556
Hans Verkuil882084a2012-09-20 09:06:31 -03001557 return v4l2_subdev_call(ch->sd, core, g_register, reg);
Mats Randgaard7036d6a2010-12-16 12:17:41 -03001558}
1559
1560/*
1561 * vpif_dbg_s_register() - Write to register
1562 * @file: file ptr
1563 * @priv: file handle
1564 * @reg: register to be modified
1565 *
1566 * Debugging only
1567 * Returns zero or -EINVAL if write operations fails.
1568 */
1569static int vpif_dbg_s_register(struct file *file, void *priv,
1570 struct v4l2_dbg_register *reg){
1571 struct vpif_fh *fh = priv;
1572 struct channel_obj *ch = fh->channel;
Mats Randgaard7036d6a2010-12-16 12:17:41 -03001573
Hans Verkuil882084a2012-09-20 09:06:31 -03001574 return v4l2_subdev_call(ch->sd, core, s_register, reg);
Mats Randgaard7036d6a2010-12-16 12:17:41 -03001575}
1576#endif
1577
1578/*
1579 * vpif_log_status() - Status information
1580 * @file: file ptr
1581 * @priv: file handle
1582 *
1583 * Returns zero.
1584 */
1585static int vpif_log_status(struct file *filep, void *priv)
1586{
1587 /* status for sub devices */
1588 v4l2_device_call_all(&vpif_obj.v4l2_dev, 0, core, log_status);
1589
1590 return 0;
1591}
1592
Chaithrika U Se7332e32009-06-09 05:55:37 -03001593/* vpif display ioctl operations */
1594static const struct v4l2_ioctl_ops vpif_ioctl_ops = {
1595 .vidioc_querycap = vpif_querycap,
1596 .vidioc_g_priority = vpif_g_priority,
1597 .vidioc_s_priority = vpif_s_priority,
1598 .vidioc_enum_fmt_vid_out = vpif_enum_fmt_vid_out,
1599 .vidioc_g_fmt_vid_out = vpif_g_fmt_vid_out,
1600 .vidioc_s_fmt_vid_out = vpif_s_fmt_vid_out,
1601 .vidioc_try_fmt_vid_out = vpif_try_fmt_vid_out,
1602 .vidioc_reqbufs = vpif_reqbufs,
1603 .vidioc_querybuf = vpif_querybuf,
1604 .vidioc_qbuf = vpif_qbuf,
1605 .vidioc_dqbuf = vpif_dqbuf,
1606 .vidioc_streamon = vpif_streamon,
1607 .vidioc_streamoff = vpif_streamoff,
1608 .vidioc_s_std = vpif_s_std,
1609 .vidioc_g_std = vpif_g_std,
1610 .vidioc_enum_output = vpif_enum_output,
1611 .vidioc_s_output = vpif_s_output,
1612 .vidioc_g_output = vpif_g_output,
1613 .vidioc_cropcap = vpif_cropcap,
Hans Verkuil0598c172012-09-18 07:18:47 -03001614 .vidioc_enum_dv_timings = vpif_enum_dv_timings,
Mats Randgaardc027e162010-12-16 12:17:44 -03001615 .vidioc_s_dv_timings = vpif_s_dv_timings,
1616 .vidioc_g_dv_timings = vpif_g_dv_timings,
Mats Randgaard7036d6a2010-12-16 12:17:41 -03001617 .vidioc_g_chip_ident = vpif_g_chip_ident,
1618#ifdef CONFIG_VIDEO_ADV_DEBUG
1619 .vidioc_g_register = vpif_dbg_g_register,
1620 .vidioc_s_register = vpif_dbg_s_register,
1621#endif
1622 .vidioc_log_status = vpif_log_status,
Chaithrika U Se7332e32009-06-09 05:55:37 -03001623};
1624
1625static const struct v4l2_file_operations vpif_fops = {
1626 .owner = THIS_MODULE,
1627 .open = vpif_open,
1628 .release = vpif_release,
Hans Verkuil9bfaae22011-01-05 13:35:45 -03001629 .unlocked_ioctl = video_ioctl2,
Chaithrika U Se7332e32009-06-09 05:55:37 -03001630 .mmap = vpif_mmap,
1631 .poll = vpif_poll
1632};
1633
1634static struct video_device vpif_video_template = {
1635 .name = "vpif",
1636 .fops = &vpif_fops,
Chaithrika U Se7332e32009-06-09 05:55:37 -03001637 .ioctl_ops = &vpif_ioctl_ops,
Chaithrika U Se7332e32009-06-09 05:55:37 -03001638};
1639
1640/*Configure the channels, buffer sizei, request irq */
1641static int initialize_vpif(void)
1642{
1643 int free_channel_objects_index;
1644 int free_buffer_channel_index;
1645 int free_buffer_index;
1646 int err = 0, i, j;
1647
1648 /* Default number of buffers should be 3 */
1649 if ((ch2_numbuffers > 0) &&
1650 (ch2_numbuffers < config_params.min_numbuffers))
1651 ch2_numbuffers = config_params.min_numbuffers;
1652 if ((ch3_numbuffers > 0) &&
1653 (ch3_numbuffers < config_params.min_numbuffers))
1654 ch3_numbuffers = config_params.min_numbuffers;
1655
1656 /* Set buffer size to min buffers size if invalid buffer size is
1657 * given */
1658 if (ch2_bufsize < config_params.min_bufsize[VPIF_CHANNEL2_VIDEO])
1659 ch2_bufsize =
1660 config_params.min_bufsize[VPIF_CHANNEL2_VIDEO];
1661 if (ch3_bufsize < config_params.min_bufsize[VPIF_CHANNEL3_VIDEO])
1662 ch3_bufsize =
1663 config_params.min_bufsize[VPIF_CHANNEL3_VIDEO];
1664
1665 config_params.numbuffers[VPIF_CHANNEL2_VIDEO] = ch2_numbuffers;
1666
1667 if (ch2_numbuffers) {
1668 config_params.channel_bufsize[VPIF_CHANNEL2_VIDEO] =
1669 ch2_bufsize;
1670 }
1671 config_params.numbuffers[VPIF_CHANNEL3_VIDEO] = ch3_numbuffers;
1672
1673 if (ch3_numbuffers) {
1674 config_params.channel_bufsize[VPIF_CHANNEL3_VIDEO] =
1675 ch3_bufsize;
1676 }
1677
1678 /* Allocate memory for six channel objects */
1679 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1680 vpif_obj.dev[i] =
Mats Randgaard1f8766b2010-08-30 10:30:37 -03001681 kzalloc(sizeof(struct channel_obj), GFP_KERNEL);
Chaithrika U Se7332e32009-06-09 05:55:37 -03001682 /* If memory allocation fails, return error */
1683 if (!vpif_obj.dev[i]) {
1684 free_channel_objects_index = i;
1685 err = -ENOMEM;
1686 goto vpif_init_free_channel_objects;
1687 }
1688 }
1689
1690 free_channel_objects_index = VPIF_DISPLAY_MAX_DEVICES;
1691 free_buffer_channel_index = VPIF_DISPLAY_NUM_CHANNELS;
1692 free_buffer_index = config_params.numbuffers[i - 1];
1693
1694 return 0;
1695
1696vpif_init_free_channel_objects:
1697 for (j = 0; j < free_channel_objects_index; j++)
1698 kfree(vpif_obj.dev[j]);
1699 return err;
1700}
1701
1702/*
1703 * vpif_probe: This function creates device entries by register itself to the
1704 * V4L2 driver and initializes fields of each channel objects
1705 */
1706static __init int vpif_probe(struct platform_device *pdev)
1707{
Muralidharan Karicheri317b2e22009-09-16 14:30:53 -03001708 struct vpif_subdev_info *subdevdata;
1709 struct vpif_display_config *config;
Hans Verkuil01b1d9752012-09-20 09:06:28 -03001710 int i, j = 0, k, err = 0;
1711 int res_idx = 0;
Chaithrika U Se7332e32009-06-09 05:55:37 -03001712 struct i2c_adapter *i2c_adap;
Chaithrika U Se7332e32009-06-09 05:55:37 -03001713 struct common_obj *common;
1714 struct channel_obj *ch;
1715 struct video_device *vfd;
1716 struct resource *res;
1717 int subdev_count;
Manjunath Hadlifc613d42012-04-13 04:49:10 -03001718 size_t size;
Chaithrika U Se7332e32009-06-09 05:55:37 -03001719
1720 vpif_dev = &pdev->dev;
Muralidharan Karicheri317b2e22009-09-16 14:30:53 -03001721 err = initialize_vpif();
1722
1723 if (err) {
1724 v4l2_err(vpif_dev->driver, "Error initializing vpif\n");
1725 return err;
Chaithrika U Se7332e32009-06-09 05:55:37 -03001726 }
1727
Chaithrika U Se7332e32009-06-09 05:55:37 -03001728 err = v4l2_device_register(vpif_dev, &vpif_obj.v4l2_dev);
1729 if (err) {
1730 v4l2_err(vpif_dev->driver, "Error registering v4l2 device\n");
1731 return err;
1732 }
1733
Hans Verkuil01b1d9752012-09-20 09:06:28 -03001734 while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, res_idx))) {
Chaithrika U Se7332e32009-06-09 05:55:37 -03001735 for (i = res->start; i <= res->end; i++) {
Manjunath Hadli0316b892012-04-13 04:44:31 -03001736 if (request_irq(i, vpif_channel_isr, IRQF_SHARED,
Hans Verkuil01b1d9752012-09-20 09:06:28 -03001737 "VPIF_Display", (void *)
1738 (&vpif_obj.dev[res_idx]->channel_id))) {
Chaithrika U Se7332e32009-06-09 05:55:37 -03001739 err = -EBUSY;
Hans Verkuil01b1d9752012-09-20 09:06:28 -03001740 for (j = 0; j < i; j++)
1741 free_irq(j, (void *)
1742 (&vpif_obj.dev[res_idx]->channel_id));
Chaithrika U Se7332e32009-06-09 05:55:37 -03001743 goto vpif_int_err;
1744 }
1745 }
Hans Verkuil01b1d9752012-09-20 09:06:28 -03001746 res_idx++;
Chaithrika U Se7332e32009-06-09 05:55:37 -03001747 }
1748
1749 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
Chaithrika U Se7332e32009-06-09 05:55:37 -03001750 /* Get the pointer to the channel object */
1751 ch = vpif_obj.dev[i];
1752
1753 /* Allocate memory for video device */
1754 vfd = video_device_alloc();
1755 if (vfd == NULL) {
1756 for (j = 0; j < i; j++) {
1757 ch = vpif_obj.dev[j];
1758 video_device_release(ch->video_dev);
1759 }
1760 err = -ENOMEM;
Muralidharan Karicheri317b2e22009-09-16 14:30:53 -03001761 goto vpif_int_err;
Chaithrika U Se7332e32009-06-09 05:55:37 -03001762 }
1763
1764 /* Initialize field of video device */
1765 *vfd = vpif_video_template;
1766 vfd->v4l2_dev = &vpif_obj.v4l2_dev;
1767 vfd->release = video_device_release;
Hans Verkuil954f3402012-09-05 06:05:50 -03001768 vfd->vfl_dir = VFL_DIR_TX;
Chaithrika U Se7332e32009-06-09 05:55:37 -03001769 snprintf(vfd->name, sizeof(vfd->name),
Manjunath Hadli0a631722012-04-13 04:44:00 -03001770 "VPIF_Display_DRIVER_V%s",
Mauro Carvalho Chehab64dc3c12011-06-25 11:28:37 -03001771 VPIF_DISPLAY_VERSION);
Chaithrika U Se7332e32009-06-09 05:55:37 -03001772
1773 /* Set video_dev to the video device */
1774 ch->video_dev = vfd;
1775 }
1776
Manjunath Hadlifc613d42012-04-13 04:49:10 -03001777 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1778 if (res) {
1779 size = resource_size(res);
1780 /* The resources are divided into two equal memory and when
1781 * we have HD output we can add them together
1782 */
1783 for (j = 0; j < VPIF_DISPLAY_MAX_DEVICES; j++) {
1784 ch = vpif_obj.dev[j];
1785 ch->channel_id = j;
1786
1787 /* only enabled if second resource exists */
1788 config_params.video_limit[ch->channel_id] = 0;
1789 if (size)
1790 config_params.video_limit[ch->channel_id] =
1791 size/2;
1792 }
1793 }
1794
Hans Verkuile6067f82012-09-20 09:06:27 -03001795 i2c_adap = i2c_get_adapter(1);
1796 config = pdev->dev.platform_data;
1797 subdev_count = config->subdev_count;
1798 subdevdata = config->subdevinfo;
1799 vpif_obj.sd = kzalloc(sizeof(struct v4l2_subdev *) * subdev_count,
1800 GFP_KERNEL);
1801 if (vpif_obj.sd == NULL) {
1802 vpif_err("unable to allocate memory for subdevice pointers\n");
1803 err = -ENOMEM;
Hans Verkuil01b1d9752012-09-20 09:06:28 -03001804 goto vpif_sd_error;
Hans Verkuile6067f82012-09-20 09:06:27 -03001805 }
1806
1807 for (i = 0; i < subdev_count; i++) {
1808 vpif_obj.sd[i] = v4l2_i2c_new_subdev_board(&vpif_obj.v4l2_dev,
1809 i2c_adap,
1810 &subdevdata[i].board_info,
1811 NULL);
1812 if (!vpif_obj.sd[i]) {
1813 vpif_err("Error registering v4l2 subdevice\n");
1814 goto probe_subdev_out;
1815 }
1816
1817 if (vpif_obj.sd[i])
1818 vpif_obj.sd[i]->grp_id = 1 << i;
1819 }
1820
Chaithrika U Se7332e32009-06-09 05:55:37 -03001821 for (j = 0; j < VPIF_DISPLAY_MAX_DEVICES; j++) {
1822 ch = vpif_obj.dev[j];
1823 /* Initialize field of the channel objects */
1824 atomic_set(&ch->usrs, 0);
1825 for (k = 0; k < VPIF_NUMOBJECTS; k++) {
1826 ch->common[k].numbuffers = 0;
1827 common = &ch->common[k];
1828 common->io_usrs = 0;
1829 common->started = 0;
1830 spin_lock_init(&common->irqlock);
1831 mutex_init(&common->lock);
1832 common->numbuffers = 0;
1833 common->set_addr = NULL;
1834 common->ytop_off = common->ybtm_off = 0;
1835 common->ctop_off = common->cbtm_off = 0;
1836 common->cur_frm = common->next_frm = NULL;
1837 memset(&common->fmt, 0, sizeof(common->fmt));
1838 common->numbuffers = config_params.numbuffers[k];
1839
1840 }
1841 ch->initialized = 0;
Hans Verkuil882084a2012-09-20 09:06:31 -03001842 if (subdev_count)
1843 ch->sd = vpif_obj.sd[0];
Chaithrika U Se7332e32009-06-09 05:55:37 -03001844 ch->channel_id = j;
1845 if (j < 2)
1846 ch->common[VPIF_VIDEO_INDEX].numbuffers =
1847 config_params.numbuffers[ch->channel_id];
1848 else
1849 ch->common[VPIF_VIDEO_INDEX].numbuffers = 0;
1850
1851 memset(&ch->vpifparams, 0, sizeof(ch->vpifparams));
1852
1853 /* Initialize prio member of channel object */
1854 v4l2_prio_init(&ch->prio);
1855 ch->common[VPIF_VIDEO_INDEX].fmt.type =
1856 V4L2_BUF_TYPE_VIDEO_OUTPUT;
Hans Verkuil9bfaae22011-01-05 13:35:45 -03001857 ch->video_dev->lock = &common->lock;
Hans Verkuile6067f82012-09-20 09:06:27 -03001858 video_set_drvdata(ch->video_dev, ch);
Chaithrika U Se7332e32009-06-09 05:55:37 -03001859
Lad, Prabhakar2bd4e582012-09-25 08:11:49 -03001860 /* select output 0 */
1861 err = vpif_set_output(config, ch, 0);
1862 if (err)
1863 goto probe_out;
1864
Chaithrika U Se7332e32009-06-09 05:55:37 -03001865 /* register video device */
1866 vpif_dbg(1, debug, "channel=%x,channel->video_dev=%x\n",
1867 (int)ch, (int)&ch->video_dev);
1868
1869 err = video_register_device(ch->video_dev,
1870 VFL_TYPE_GRABBER, (j ? 3 : 2));
1871 if (err < 0)
1872 goto probe_out;
Chaithrika U Se7332e32009-06-09 05:55:37 -03001873 }
1874
Mats Randgaard2c0ddd12010-12-16 12:17:45 -03001875 v4l2_info(&vpif_obj.v4l2_dev,
Manjunath Hadli0a631722012-04-13 04:44:00 -03001876 " VPIF display driver initialized\n");
Chaithrika U Se7332e32009-06-09 05:55:37 -03001877 return 0;
1878
Chaithrika U Se7332e32009-06-09 05:55:37 -03001879probe_out:
1880 for (k = 0; k < j; k++) {
1881 ch = vpif_obj.dev[k];
1882 video_unregister_device(ch->video_dev);
1883 video_device_release(ch->video_dev);
1884 ch->video_dev = NULL;
1885 }
Hans Verkuile6067f82012-09-20 09:06:27 -03001886probe_subdev_out:
1887 kfree(vpif_obj.sd);
Hans Verkuil01b1d9752012-09-20 09:06:28 -03001888vpif_sd_error:
1889 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1890 ch = vpif_obj.dev[i];
1891 /* Note: does nothing if ch->video_dev == NULL */
1892 video_device_release(ch->video_dev);
1893 }
Chaithrika U Se7332e32009-06-09 05:55:37 -03001894vpif_int_err:
1895 v4l2_device_unregister(&vpif_obj.v4l2_dev);
1896 vpif_err("VPIF IRQ request failed\n");
Hans Verkuil01b1d9752012-09-20 09:06:28 -03001897 for (i = 0; i < res_idx; i++) {
1898 res = platform_get_resource(pdev, IORESOURCE_IRQ, i);
1899 for (j = res->start; j <= res->end; j++)
1900 free_irq(j, (void *)(&vpif_obj.dev[i]->channel_id));
Chaithrika U Se7332e32009-06-09 05:55:37 -03001901 }
Chaithrika U Se7332e32009-06-09 05:55:37 -03001902
1903 return err;
1904}
1905
1906/*
1907 * vpif_remove: It un-register channels from V4L2 driver
1908 */
1909static int vpif_remove(struct platform_device *device)
1910{
1911 struct channel_obj *ch;
1912 int i;
1913
1914 v4l2_device_unregister(&vpif_obj.v4l2_dev);
1915
1916 /* un-register device */
1917 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1918 /* Get the pointer to the channel object */
1919 ch = vpif_obj.dev[i];
1920 /* Unregister video device */
1921 video_unregister_device(ch->video_dev);
1922
1923 ch->video_dev = NULL;
1924 }
1925
1926 return 0;
1927}
1928
Manjunath Hadlie9530da2012-04-13 04:50:35 -03001929#ifdef CONFIG_PM
1930static int vpif_suspend(struct device *dev)
1931{
1932 struct common_obj *common;
1933 struct channel_obj *ch;
1934 int i;
1935
1936 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1937 /* Get the pointer to the channel object */
1938 ch = vpif_obj.dev[i];
1939 common = &ch->common[VPIF_VIDEO_INDEX];
1940 mutex_lock(&common->lock);
1941 if (atomic_read(&ch->usrs) && common->io_usrs) {
1942 /* Disable channel */
1943 if (ch->channel_id == VPIF_CHANNEL2_VIDEO) {
1944 enable_channel2(0);
1945 channel2_intr_enable(0);
1946 }
1947 if (ch->channel_id == VPIF_CHANNEL3_VIDEO ||
1948 common->started == 2) {
1949 enable_channel3(0);
1950 channel3_intr_enable(0);
1951 }
1952 }
1953 mutex_unlock(&common->lock);
1954 }
1955
1956 return 0;
1957}
1958
1959static int vpif_resume(struct device *dev)
1960{
1961
1962 struct common_obj *common;
1963 struct channel_obj *ch;
1964 int i;
1965
1966 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1967 /* Get the pointer to the channel object */
1968 ch = vpif_obj.dev[i];
1969 common = &ch->common[VPIF_VIDEO_INDEX];
1970 mutex_lock(&common->lock);
1971 if (atomic_read(&ch->usrs) && common->io_usrs) {
1972 /* Enable channel */
1973 if (ch->channel_id == VPIF_CHANNEL2_VIDEO) {
1974 enable_channel2(1);
1975 channel2_intr_enable(1);
1976 }
1977 if (ch->channel_id == VPIF_CHANNEL3_VIDEO ||
1978 common->started == 2) {
1979 enable_channel3(1);
1980 channel3_intr_enable(1);
1981 }
1982 }
1983 mutex_unlock(&common->lock);
1984 }
1985
1986 return 0;
1987}
1988
1989static const struct dev_pm_ops vpif_pm = {
1990 .suspend = vpif_suspend,
1991 .resume = vpif_resume,
1992};
1993
1994#define vpif_pm_ops (&vpif_pm)
1995#else
1996#define vpif_pm_ops NULL
1997#endif
1998
Mats Randgaardffa1b392010-08-30 10:30:36 -03001999static __refdata struct platform_driver vpif_driver = {
Chaithrika U Se7332e32009-06-09 05:55:37 -03002000 .driver = {
2001 .name = "vpif_display",
2002 .owner = THIS_MODULE,
Manjunath Hadlie9530da2012-04-13 04:50:35 -03002003 .pm = vpif_pm_ops,
Chaithrika U Se7332e32009-06-09 05:55:37 -03002004 },
2005 .probe = vpif_probe,
2006 .remove = vpif_remove,
2007};
2008
2009static __init int vpif_init(void)
2010{
2011 return platform_driver_register(&vpif_driver);
2012}
2013
2014/*
2015 * vpif_cleanup: This function un-registers device and driver to the kernel,
2016 * frees requested irq handler and de-allocates memory allocated for channel
2017 * objects.
2018 */
2019static void vpif_cleanup(void)
2020{
2021 struct platform_device *pdev;
2022 struct resource *res;
2023 int irq_num;
2024 int i = 0;
2025
2026 pdev = container_of(vpif_dev, struct platform_device, dev);
2027
2028 while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, i))) {
2029 for (irq_num = res->start; irq_num <= res->end; irq_num++)
2030 free_irq(irq_num,
2031 (void *)(&vpif_obj.dev[i]->channel_id));
2032 i++;
2033 }
2034
Chaithrika U Se7332e32009-06-09 05:55:37 -03002035 platform_driver_unregister(&vpif_driver);
2036 kfree(vpif_obj.sd);
2037 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++)
2038 kfree(vpif_obj.dev[i]);
2039}
2040
2041module_init(vpif_init);
2042module_exit(vpif_cleanup);