blob: f51518c5b7879b81b628abde01866d13cf7a59b8 [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/
Lad, Prabhakar96787eb2014-05-16 10:33:55 -03006 * Copyright (C) 2014 Lad, Prabhakar <prabhakar.csengg@gmail.com>
Chaithrika U Se7332e32009-06-09 05:55:37 -03007 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation version 2.
11 *
12 * This program is distributed .as is. WITHOUT ANY WARRANTY of any
13 * kind, whether express or implied; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 */
17
Chaithrika U Se7332e32009-06-09 05:55:37 -030018#include <linux/interrupt.h>
Lad, Prabhakar012eef72013-04-19 05:53:29 -030019#include <linux/module.h>
Chaithrika U Se7332e32009-06-09 05:55:37 -030020#include <linux/platform_device.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090021#include <linux/slab.h>
Chaithrika U Se7332e32009-06-09 05:55:37 -030022
Lad, Prabhakar012eef72013-04-19 05:53:29 -030023#include <media/v4l2-ioctl.h>
Chaithrika U Se7332e32009-06-09 05:55:37 -030024
Chaithrika U Se7332e32009-06-09 05:55:37 -030025#include "vpif.h"
Lad, Prabhakar012eef72013-04-19 05:53:29 -030026#include "vpif_display.h"
Chaithrika U Se7332e32009-06-09 05:55:37 -030027
28MODULE_DESCRIPTION("TI DaVinci VPIF Display driver");
29MODULE_LICENSE("GPL");
Mauro Carvalho Chehab64dc3c12011-06-25 11:28:37 -030030MODULE_VERSION(VPIF_DISPLAY_VERSION);
Chaithrika U Se7332e32009-06-09 05:55:37 -030031
Manjunath Hadli0a631722012-04-13 04:44:00 -030032#define VPIF_V4L2_STD (V4L2_STD_525_60 | V4L2_STD_625_50)
Chaithrika U Se7332e32009-06-09 05:55:37 -030033
34#define vpif_err(fmt, arg...) v4l2_err(&vpif_obj.v4l2_dev, fmt, ## arg)
35#define vpif_dbg(level, debug, fmt, arg...) \
36 v4l2_dbg(level, debug, &vpif_obj.v4l2_dev, fmt, ## arg)
37
38static int debug = 1;
Chaithrika U Se7332e32009-06-09 05:55:37 -030039
40module_param(debug, int, 0644);
Chaithrika U Se7332e32009-06-09 05:55:37 -030041
42MODULE_PARM_DESC(debug, "Debug level 0-1");
Chaithrika U Se7332e32009-06-09 05:55:37 -030043
Lad, Prabhakar76c4c2b2014-05-16 10:33:22 -030044#define VPIF_DRIVER_NAME "vpif_display"
Lad, Prabhakar2f5851b2014-05-16 10:33:21 -030045
46/* Is set to 1 in case of SDTV formats, 2 in case of HDTV formats. */
47static int ycmux_mode;
48
Lad, Prabhakar58334b02014-05-16 10:33:13 -030049static u8 channel_first_int[VPIF_NUMOBJECTS][2] = { {1, 1} };
50
Chaithrika U Se7332e32009-06-09 05:55:37 -030051static struct vpif_device vpif_obj = { {NULL} };
52static struct device *vpif_dev;
Lad, Prabhakar2401dd22012-06-28 09:28:36 -030053static void vpif_calculate_offsets(struct channel_obj *ch);
54static void vpif_config_addr(struct channel_obj *ch, int muxmode);
Chaithrika U Se7332e32009-06-09 05:55:37 -030055
Junghak Sung2d700712015-09-22 10:30:30 -030056static inline
57struct vpif_disp_buffer *to_vpif_buffer(struct vb2_v4l2_buffer *vb)
Lad, Prabhakar58334b02014-05-16 10:33:13 -030058{
59 return container_of(vb, struct vpif_disp_buffer, vb);
60}
61
Lad, Prabhakar27cdf9b2014-05-16 10:33:11 -030062/**
63 * vpif_buffer_prepare : callback function for buffer prepare
64 * @vb: ptr to vb2_buffer
65 *
66 * This is the callback function for buffer prepare when vb2_qbuf()
67 * function is called. The buffer is prepared and user space virtual address
68 * or user address is converted into physical address
Chaithrika U Se7332e32009-06-09 05:55:37 -030069 */
Lad, Prabhakar2401dd22012-06-28 09:28:36 -030070static int vpif_buffer_prepare(struct vb2_buffer *vb)
Chaithrika U Se7332e32009-06-09 05:55:37 -030071{
Junghak Sung2d700712015-09-22 10:30:30 -030072 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
Lad, Prabhakar27cdf9b2014-05-16 10:33:11 -030073 struct channel_obj *ch = vb2_get_drv_priv(vb->vb2_queue);
Chaithrika U Se7332e32009-06-09 05:55:37 -030074 struct common_obj *common;
Chaithrika U Se7332e32009-06-09 05:55:37 -030075
Lad, Prabhakara2b235c2014-05-16 10:33:06 -030076 common = &ch->common[VPIF_VIDEO_INDEX];
Chaithrika U Se7332e32009-06-09 05:55:37 -030077
Lad, Prabhakar27cdf9b2014-05-16 10:33:11 -030078 vb2_set_plane_payload(vb, 0, common->fmt.fmt.pix.sizeimage);
79 if (vb2_get_plane_payload(vb, 0) > vb2_plane_size(vb, 0))
80 return -EINVAL;
81
Junghak Sung2d700712015-09-22 10:30:30 -030082 vbuf->field = common->fmt.fmt.pix.field;
Lad, Prabhakar27cdf9b2014-05-16 10:33:11 -030083
84 if (vb->vb2_queue->type != V4L2_BUF_TYPE_SLICED_VBI_OUTPUT) {
85 unsigned long addr = vb2_dma_contig_plane_dma_addr(vb, 0);
86
87 if (!ISALIGNED(addr + common->ytop_off) ||
Lad, Prabhakar2401dd22012-06-28 09:28:36 -030088 !ISALIGNED(addr + common->ybtm_off) ||
89 !ISALIGNED(addr + common->ctop_off) ||
Lad, Prabhakar27cdf9b2014-05-16 10:33:11 -030090 !ISALIGNED(addr + common->cbtm_off)) {
91 vpif_err("buffer offset not aligned to 8 bytes\n");
92 return -EINVAL;
Chaithrika U Se7332e32009-06-09 05:55:37 -030093 }
Chaithrika U Se7332e32009-06-09 05:55:37 -030094 }
Chaithrika U Se7332e32009-06-09 05:55:37 -030095
Lad, Prabhakar27cdf9b2014-05-16 10:33:11 -030096 return 0;
Chaithrika U Se7332e32009-06-09 05:55:37 -030097}
98
Lad, Prabhakar172392a2014-05-16 10:33:12 -030099/**
100 * vpif_buffer_queue_setup : Callback function for buffer setup.
101 * @vq: vb2_queue ptr
102 * @fmt: v4l2 format
103 * @nbuffers: ptr to number of buffers requested by application
104 * @nplanes:: contains number of distinct video planes needed to hold a frame
105 * @sizes[]: contains the size (in bytes) of each plane.
106 * @alloc_ctxs: ptr to allocation context
107 *
108 * This callback function is called when reqbuf() is called to adjust
109 * the buffer count and buffer size
Chaithrika U Se7332e32009-06-09 05:55:37 -0300110 */
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300111static int vpif_buffer_queue_setup(struct vb2_queue *vq,
112 const struct v4l2_format *fmt,
113 unsigned int *nbuffers, unsigned int *nplanes,
114 unsigned int sizes[], void *alloc_ctxs[])
Chaithrika U Se7332e32009-06-09 05:55:37 -0300115{
Lad, Prabhakara2b235c2014-05-16 10:33:06 -0300116 struct channel_obj *ch = vb2_get_drv_priv(vq);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300117 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
118
Lad, Prabhakar172392a2014-05-16 10:33:12 -0300119 if (fmt && fmt->fmt.pix.sizeimage < common->fmt.fmt.pix.sizeimage)
120 return -EINVAL;
Manjunath Hadlifc613d42012-04-13 04:49:10 -0300121
Lad, Prabhakar172392a2014-05-16 10:33:12 -0300122 if (vq->num_buffers + *nbuffers < 3)
123 *nbuffers = 3 - vq->num_buffers;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300124
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300125 *nplanes = 1;
Lad, Prabhakar172392a2014-05-16 10:33:12 -0300126 sizes[0] = fmt ? fmt->fmt.pix.sizeimage : common->fmt.fmt.pix.sizeimage;
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300127 alloc_ctxs[0] = common->alloc_ctx;
Lad, Prabhakar172392a2014-05-16 10:33:12 -0300128
129 /* Calculate the offset for Y and C data in the buffer */
130 vpif_calculate_offsets(ch);
131
Chaithrika U Se7332e32009-06-09 05:55:37 -0300132 return 0;
133}
134
Lad, Prabhakar58334b02014-05-16 10:33:13 -0300135/**
136 * vpif_buffer_queue : Callback function to add buffer to DMA queue
137 * @vb: ptr to vb2_buffer
138 *
139 * This callback fucntion queues the buffer to DMA engine
Chaithrika U Se7332e32009-06-09 05:55:37 -0300140 */
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300141static void vpif_buffer_queue(struct vb2_buffer *vb)
Chaithrika U Se7332e32009-06-09 05:55:37 -0300142{
Junghak Sung2d700712015-09-22 10:30:30 -0300143 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
144 struct vpif_disp_buffer *buf = to_vpif_buffer(vbuf);
Lad, Prabhakara2b235c2014-05-16 10:33:06 -0300145 struct channel_obj *ch = vb2_get_drv_priv(vb->vb2_queue);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300146 struct common_obj *common;
Hans Verkuilc4697d72012-11-16 12:03:07 -0300147 unsigned long flags;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300148
149 common = &ch->common[VPIF_VIDEO_INDEX];
150
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300151 /* add the buffer to the DMA queue */
Hans Verkuilc4697d72012-11-16 12:03:07 -0300152 spin_lock_irqsave(&common->irqlock, flags);
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300153 list_add_tail(&buf->list, &common->dma_queue);
Hans Verkuilc4697d72012-11-16 12:03:07 -0300154 spin_unlock_irqrestore(&common->irqlock, flags);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300155}
156
Lad, Prabhakar58334b02014-05-16 10:33:13 -0300157/**
158 * vpif_start_streaming : Starts the DMA engine for streaming
159 * @vb: ptr to vb2_buffer
160 * @count: number of buffers
161 */
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300162static int vpif_start_streaming(struct vb2_queue *vq, unsigned int count)
163{
164 struct vpif_display_config *vpif_config_data =
165 vpif_dev->platform_data;
Lad, Prabhakara2b235c2014-05-16 10:33:06 -0300166 struct channel_obj *ch = vb2_get_drv_priv(vq);
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300167 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
168 struct vpif_params *vpif = &ch->vpifparams;
Lad, Prabhakar54a445a42014-05-16 10:33:09 -0300169 struct vpif_disp_buffer *buf, *tmp;
170 unsigned long addr, flags;
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300171 int ret;
172
Hans Verkuilc4697d72012-11-16 12:03:07 -0300173 spin_lock_irqsave(&common->irqlock, flags);
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300174
Lad, Prabhakar2f5851b2014-05-16 10:33:21 -0300175 /* Initialize field_id */
Lad, Prabhakar54a445a42014-05-16 10:33:09 -0300176 ch->field_id = 0;
Lad, Prabhakar54a445a42014-05-16 10:33:09 -0300177
Lad, Prabhakar54a445a42014-05-16 10:33:09 -0300178 /* clock settings */
179 if (vpif_config_data->set_clock) {
180 ret = vpif_config_data->set_clock(ch->vpifparams.std_info.
181 ycmux_mode, ch->vpifparams.std_info.hd_sd);
182 if (ret < 0) {
183 vpif_err("can't set clock\n");
184 goto err;
185 }
186 }
187
188 /* set the parameters and addresses */
189 ret = vpif_set_video_params(vpif, ch->channel_id + 2);
190 if (ret < 0)
191 goto err;
192
Lad, Prabhakar2f5851b2014-05-16 10:33:21 -0300193 ycmux_mode = ret;
Lad, Prabhakar54a445a42014-05-16 10:33:09 -0300194 vpif_config_addr(ch, ret);
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300195 /* Get the next frame from the buffer queue */
196 common->next_frm = common->cur_frm =
197 list_entry(common->dma_queue.next,
198 struct vpif_disp_buffer, list);
199
200 list_del(&common->cur_frm->list);
Hans Verkuilc4697d72012-11-16 12:03:07 -0300201 spin_unlock_irqrestore(&common->irqlock, flags);
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300202
Junghak Sung2d700712015-09-22 10:30:30 -0300203 addr = vb2_dma_contig_plane_dma_addr(&common->cur_frm->vb.vb2_buf, 0);
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300204 common->set_addr((addr + common->ytop_off),
205 (addr + common->ybtm_off),
206 (addr + common->ctop_off),
207 (addr + common->cbtm_off));
208
Lad, Prabhakar58334b02014-05-16 10:33:13 -0300209 /*
210 * Set interrupt for both the fields in VPIF
211 * Register enable channel in VPIF register
212 */
Lad, Prabhakar9e184042012-09-14 10:22:24 -0300213 channel_first_int[VPIF_VIDEO_INDEX][ch->channel_id] = 1;
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300214 if (VPIF_CHANNEL2_VIDEO == ch->channel_id) {
215 channel2_intr_assert();
216 channel2_intr_enable(1);
217 enable_channel2(1);
Lad, Prabhakar2bd4e582012-09-25 08:11:49 -0300218 if (vpif_config_data->chan_config[VPIF_CHANNEL2_VIDEO].clip_en)
Manjunath Hadli6964b102012-06-29 03:20:12 -0300219 channel2_clipping_enable(1);
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300220 }
221
Lad, Prabhakar2f5851b2014-05-16 10:33:21 -0300222 if (VPIF_CHANNEL3_VIDEO == ch->channel_id || ycmux_mode == 2) {
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300223 channel3_intr_assert();
224 channel3_intr_enable(1);
225 enable_channel3(1);
Lad, Prabhakar2bd4e582012-09-25 08:11:49 -0300226 if (vpif_config_data->chan_config[VPIF_CHANNEL3_VIDEO].clip_en)
Manjunath Hadli6964b102012-06-29 03:20:12 -0300227 channel3_clipping_enable(1);
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300228 }
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300229
230 return 0;
Lad, Prabhakar54a445a42014-05-16 10:33:09 -0300231
232err:
233 list_for_each_entry_safe(buf, tmp, &common->dma_queue, list) {
234 list_del(&buf->list);
Junghak Sung2d700712015-09-22 10:30:30 -0300235 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_QUEUED);
Lad, Prabhakar54a445a42014-05-16 10:33:09 -0300236 }
Dan Carpenter92491962014-06-12 04:01:45 -0300237 spin_unlock_irqrestore(&common->irqlock, flags);
Lad, Prabhakar54a445a42014-05-16 10:33:09 -0300238
239 return ret;
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300240}
241
Lad, Prabhakar58334b02014-05-16 10:33:13 -0300242/**
243 * vpif_stop_streaming : Stop the DMA engine
244 * @vq: ptr to vb2_queue
245 *
246 * This callback stops the DMA engine and any remaining buffers
247 * in the DMA queue are released.
248 */
Hans Verkuile37559b2014-04-17 02:47:21 -0300249static void vpif_stop_streaming(struct vb2_queue *vq)
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300250{
Lad, Prabhakara2b235c2014-05-16 10:33:06 -0300251 struct channel_obj *ch = vb2_get_drv_priv(vq);
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300252 struct common_obj *common;
Hans Verkuilc4697d72012-11-16 12:03:07 -0300253 unsigned long flags;
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300254
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300255 common = &ch->common[VPIF_VIDEO_INDEX];
256
Lad, Prabhakar18c7adc2014-03-22 08:03:08 -0300257 /* Disable channel */
258 if (VPIF_CHANNEL2_VIDEO == ch->channel_id) {
259 enable_channel2(0);
260 channel2_intr_enable(0);
261 }
Lad, Prabhakar2f5851b2014-05-16 10:33:21 -0300262 if (VPIF_CHANNEL3_VIDEO == ch->channel_id || ycmux_mode == 2) {
Lad, Prabhakar18c7adc2014-03-22 08:03:08 -0300263 enable_channel3(0);
264 channel3_intr_enable(0);
265 }
Lad, Prabhakar18c7adc2014-03-22 08:03:08 -0300266
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300267 /* release all active buffers */
Hans Verkuilc4697d72012-11-16 12:03:07 -0300268 spin_lock_irqsave(&common->irqlock, flags);
Lad, Prabhakar18c7adc2014-03-22 08:03:08 -0300269 if (common->cur_frm == common->next_frm) {
Junghak Sung2d700712015-09-22 10:30:30 -0300270 vb2_buffer_done(&common->cur_frm->vb.vb2_buf,
271 VB2_BUF_STATE_ERROR);
Lad, Prabhakar18c7adc2014-03-22 08:03:08 -0300272 } else {
273 if (common->cur_frm != NULL)
Junghak Sung2d700712015-09-22 10:30:30 -0300274 vb2_buffer_done(&common->cur_frm->vb.vb2_buf,
Lad, Prabhakar18c7adc2014-03-22 08:03:08 -0300275 VB2_BUF_STATE_ERROR);
276 if (common->next_frm != NULL)
Junghak Sung2d700712015-09-22 10:30:30 -0300277 vb2_buffer_done(&common->next_frm->vb.vb2_buf,
Lad, Prabhakar18c7adc2014-03-22 08:03:08 -0300278 VB2_BUF_STATE_ERROR);
279 }
280
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300281 while (!list_empty(&common->dma_queue)) {
282 common->next_frm = list_entry(common->dma_queue.next,
283 struct vpif_disp_buffer, list);
284 list_del(&common->next_frm->list);
Junghak Sung2d700712015-09-22 10:30:30 -0300285 vb2_buffer_done(&common->next_frm->vb.vb2_buf,
286 VB2_BUF_STATE_ERROR);
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300287 }
Hans Verkuilc4697d72012-11-16 12:03:07 -0300288 spin_unlock_irqrestore(&common->irqlock, flags);
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300289}
290
291static struct vb2_ops video_qops = {
292 .queue_setup = vpif_buffer_queue_setup,
Lad, Prabhakard10ed5c2014-05-16 10:33:08 -0300293 .wait_prepare = vb2_ops_wait_prepare,
294 .wait_finish = vb2_ops_wait_finish,
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300295 .buf_prepare = vpif_buffer_prepare,
296 .start_streaming = vpif_start_streaming,
297 .stop_streaming = vpif_stop_streaming,
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300298 .buf_queue = vpif_buffer_queue,
299};
300
Chaithrika U Se7332e32009-06-09 05:55:37 -0300301static void process_progressive_mode(struct common_obj *common)
302{
303 unsigned long addr = 0;
304
Hans Verkuilc4697d72012-11-16 12:03:07 -0300305 spin_lock(&common->irqlock);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300306 /* Get the next buffer from buffer queue */
307 common->next_frm = list_entry(common->dma_queue.next,
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300308 struct vpif_disp_buffer, list);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300309 /* Remove that buffer from the buffer queue */
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300310 list_del(&common->next_frm->list);
Hans Verkuilc4697d72012-11-16 12:03:07 -0300311 spin_unlock(&common->irqlock);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300312
313 /* Set top and bottom field addrs in VPIF registers */
Junghak Sung2d700712015-09-22 10:30:30 -0300314 addr = vb2_dma_contig_plane_dma_addr(&common->next_frm->vb.vb2_buf, 0);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300315 common->set_addr(addr + common->ytop_off,
316 addr + common->ybtm_off,
317 addr + common->ctop_off,
318 addr + common->cbtm_off);
319}
320
321static void process_interlaced_mode(int fid, struct common_obj *common)
322{
323 /* device field id and local field id are in sync */
324 /* If this is even field */
325 if (0 == fid) {
326 if (common->cur_frm == common->next_frm)
327 return;
328
329 /* one frame is displayed If next frame is
330 * available, release cur_frm and move on */
331 /* Copy frame display time */
Junghak Sung2d700712015-09-22 10:30:30 -0300332 v4l2_get_timestamp(&common->cur_frm->vb.timestamp);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300333 /* Change status of the cur_frm */
Junghak Sung2d700712015-09-22 10:30:30 -0300334 vb2_buffer_done(&common->cur_frm->vb.vb2_buf,
335 VB2_BUF_STATE_DONE);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300336 /* Make cur_frm pointing to next_frm */
337 common->cur_frm = common->next_frm;
338
339 } else if (1 == fid) { /* odd field */
Hans Verkuilc4697d72012-11-16 12:03:07 -0300340 spin_lock(&common->irqlock);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300341 if (list_empty(&common->dma_queue)
342 || (common->cur_frm != common->next_frm)) {
Hans Verkuilc4697d72012-11-16 12:03:07 -0300343 spin_unlock(&common->irqlock);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300344 return;
345 }
Hans Verkuilc4697d72012-11-16 12:03:07 -0300346 spin_unlock(&common->irqlock);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300347 /* one field is displayed configure the next
348 * frame if it is available else hold on current
349 * frame */
350 /* Get next from the buffer queue */
351 process_progressive_mode(common);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300352 }
353}
354
355/*
356 * vpif_channel_isr: It changes status of the displayed buffer, takes next
357 * buffer from the queue and sets its address in VPIF registers
358 */
359static irqreturn_t vpif_channel_isr(int irq, void *dev_id)
360{
361 struct vpif_device *dev = &vpif_obj;
362 struct channel_obj *ch;
363 struct common_obj *common;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300364 int fid = -1, i;
365 int channel_id = 0;
366
367 channel_id = *(int *)(dev_id);
Manjunath Hadlib1fc4232012-04-13 04:43:10 -0300368 if (!vpif_intr_status(channel_id + 2))
369 return IRQ_NONE;
370
Chaithrika U Se7332e32009-06-09 05:55:37 -0300371 ch = dev->dev[channel_id];
Chaithrika U Se7332e32009-06-09 05:55:37 -0300372 for (i = 0; i < VPIF_NUMOBJECTS; i++) {
373 common = &ch->common[i];
374 /* If streaming is started in this channel */
Chaithrika U Se7332e32009-06-09 05:55:37 -0300375
376 if (1 == ch->vpifparams.std_info.frm_fmt) {
Hans Verkuilc4697d72012-11-16 12:03:07 -0300377 spin_lock(&common->irqlock);
378 if (list_empty(&common->dma_queue)) {
379 spin_unlock(&common->irqlock);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300380 continue;
Hans Verkuilc4697d72012-11-16 12:03:07 -0300381 }
382 spin_unlock(&common->irqlock);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300383
384 /* Progressive mode */
385 if (!channel_first_int[i][channel_id]) {
386 /* Mark status of the cur_frm to
387 * done and unlock semaphore on it */
Junghak Sung2d700712015-09-22 10:30:30 -0300388 v4l2_get_timestamp(
389 &common->cur_frm->vb.timestamp);
390 vb2_buffer_done(&common->cur_frm->vb.vb2_buf,
391 VB2_BUF_STATE_DONE);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300392 /* Make cur_frm pointing to next_frm */
393 common->cur_frm = common->next_frm;
394 }
395
396 channel_first_int[i][channel_id] = 0;
397 process_progressive_mode(common);
398 } else {
399 /* Interlaced mode */
400 /* If it is first interrupt, ignore it */
401
402 if (channel_first_int[i][channel_id]) {
403 channel_first_int[i][channel_id] = 0;
404 continue;
405 }
406
407 if (0 == i) {
408 ch->field_id ^= 1;
409 /* Get field id from VPIF registers */
410 fid = vpif_channel_getfid(ch->channel_id + 2);
411 /* If fid does not match with stored field id */
412 if (fid != ch->field_id) {
413 /* Make them in sync */
414 if (0 == fid)
415 ch->field_id = fid;
416
417 return IRQ_HANDLED;
418 }
419 }
420 process_interlaced_mode(fid, common);
421 }
422 }
423
424 return IRQ_HANDLED;
425}
426
Mats Randgaardc027e162010-12-16 12:17:44 -0300427static int vpif_update_std_info(struct channel_obj *ch)
Chaithrika U Se7332e32009-06-09 05:55:37 -0300428{
Chaithrika U Se7332e32009-06-09 05:55:37 -0300429 struct video_obj *vid_ch = &ch->video;
430 struct vpif_params *vpifparams = &ch->vpifparams;
431 struct vpif_channel_config_params *std_info = &vpifparams->std_info;
432 const struct vpif_channel_config_params *config;
433
Mats Randgaardc027e162010-12-16 12:17:44 -0300434 int i;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300435
Mats Randgaardc027e162010-12-16 12:17:44 -0300436 for (i = 0; i < vpif_ch_params_count; i++) {
Lad, Prabhakarced9b212013-03-20 01:28:27 -0300437 config = &vpif_ch_params[i];
Mats Randgaard40c8bce2010-12-16 12:17:43 -0300438 if (config->hd_sd == 0) {
439 vpif_dbg(2, debug, "SD format\n");
440 if (config->stdid & vid_ch->stdid) {
441 memcpy(std_info, config, sizeof(*config));
442 break;
443 }
Chaithrika U Se7332e32009-06-09 05:55:37 -0300444 }
445 }
446
Mats Randgaardc027e162010-12-16 12:17:44 -0300447 if (i == vpif_ch_params_count) {
448 vpif_dbg(1, debug, "Format not found\n");
Mats Randgaardaa444402010-12-16 12:17:42 -0300449 return -EINVAL;
Mats Randgaardc027e162010-12-16 12:17:44 -0300450 }
451
452 return 0;
453}
454
455static int vpif_update_resolution(struct channel_obj *ch)
456{
457 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
458 struct video_obj *vid_ch = &ch->video;
459 struct vpif_params *vpifparams = &ch->vpifparams;
460 struct vpif_channel_config_params *std_info = &vpifparams->std_info;
461
Hans Verkuil0598c172012-09-18 07:18:47 -0300462 if (!vid_ch->stdid && !vid_ch->dv_timings.bt.height)
Mats Randgaardc027e162010-12-16 12:17:44 -0300463 return -EINVAL;
464
Hans Verkuil0598c172012-09-18 07:18:47 -0300465 if (vid_ch->stdid) {
Mats Randgaardc027e162010-12-16 12:17:44 -0300466 if (vpif_update_std_info(ch))
467 return -EINVAL;
468 }
Chaithrika U Se7332e32009-06-09 05:55:37 -0300469
Lad, Prabhakar9cba6532014-05-16 10:33:30 -0300470 common->fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUV422P;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300471 common->fmt.fmt.pix.width = std_info->width;
472 common->fmt.fmt.pix.height = std_info->height;
473 vpif_dbg(1, debug, "Pixel details: Width = %d,Height = %d\n",
474 common->fmt.fmt.pix.width, common->fmt.fmt.pix.height);
475
476 /* Set height and width paramateres */
Mats Randgaardc027e162010-12-16 12:17:44 -0300477 common->height = std_info->height;
478 common->width = std_info->width;
Lad, Prabhakar9cba6532014-05-16 10:33:30 -0300479 common->fmt.fmt.pix.sizeimage = common->height * common->width * 2;
480
481 if (vid_ch->stdid)
482 common->fmt.fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
483 else
484 common->fmt.fmt.pix.colorspace = V4L2_COLORSPACE_REC709;
485
486 if (ch->vpifparams.std_info.frm_fmt)
487 common->fmt.fmt.pix.field = V4L2_FIELD_NONE;
488 else
489 common->fmt.fmt.pix.field = V4L2_FIELD_INTERLACED;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300490
491 return 0;
492}
493
494/*
495 * vpif_calculate_offsets: This function calculates buffers offset for Y and C
496 * in the top and bottom field
497 */
498static void vpif_calculate_offsets(struct channel_obj *ch)
499{
500 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
501 struct vpif_params *vpifparams = &ch->vpifparams;
502 enum v4l2_field field = common->fmt.fmt.pix.field;
503 struct video_obj *vid_ch = &ch->video;
Mauro Carvalho Chehaba4f20e22014-08-21 15:46:46 -0500504 unsigned int hpitch, sizeimage;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300505
506 if (V4L2_FIELD_ANY == common->fmt.fmt.pix.field) {
507 if (ch->vpifparams.std_info.frm_fmt)
508 vid_ch->buf_field = V4L2_FIELD_NONE;
509 else
510 vid_ch->buf_field = V4L2_FIELD_INTERLACED;
511 } else {
512 vid_ch->buf_field = common->fmt.fmt.pix.field;
513 }
514
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300515 sizeimage = common->fmt.fmt.pix.sizeimage;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300516
517 hpitch = common->fmt.fmt.pix.bytesperline;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300518 if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
519 (V4L2_FIELD_INTERLACED == vid_ch->buf_field)) {
520 common->ytop_off = 0;
521 common->ybtm_off = hpitch;
522 common->ctop_off = sizeimage / 2;
523 common->cbtm_off = sizeimage / 2 + hpitch;
524 } else if (V4L2_FIELD_SEQ_TB == vid_ch->buf_field) {
525 common->ytop_off = 0;
526 common->ybtm_off = sizeimage / 4;
527 common->ctop_off = sizeimage / 2;
528 common->cbtm_off = common->ctop_off + sizeimage / 4;
529 } else if (V4L2_FIELD_SEQ_BT == vid_ch->buf_field) {
530 common->ybtm_off = 0;
531 common->ytop_off = sizeimage / 4;
532 common->cbtm_off = sizeimage / 2;
533 common->ctop_off = common->cbtm_off + sizeimage / 4;
534 }
535
536 if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
537 (V4L2_FIELD_INTERLACED == vid_ch->buf_field)) {
538 vpifparams->video_params.storage_mode = 1;
539 } else {
540 vpifparams->video_params.storage_mode = 0;
541 }
542
543 if (ch->vpifparams.std_info.frm_fmt == 1) {
544 vpifparams->video_params.hpitch =
545 common->fmt.fmt.pix.bytesperline;
546 } else {
547 if ((field == V4L2_FIELD_ANY) ||
548 (field == V4L2_FIELD_INTERLACED))
549 vpifparams->video_params.hpitch =
550 common->fmt.fmt.pix.bytesperline * 2;
551 else
552 vpifparams->video_params.hpitch =
553 common->fmt.fmt.pix.bytesperline;
554 }
555
556 ch->vpifparams.video_params.stdid = ch->vpifparams.std_info.stdid;
557}
558
Chaithrika U Se7332e32009-06-09 05:55:37 -0300559static void vpif_config_addr(struct channel_obj *ch, int muxmode)
560{
561 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
562
563 if (VPIF_CHANNEL3_VIDEO == ch->channel_id) {
564 common->set_addr = ch3_set_videobuf_addr;
565 } else {
566 if (2 == muxmode)
567 common->set_addr = ch2_set_videobuf_addr_yc_nmux;
568 else
569 common->set_addr = ch2_set_videobuf_addr;
570 }
571}
572
Chaithrika U Se7332e32009-06-09 05:55:37 -0300573/* functions implementing ioctls */
Mats Randgaard2c0ddd12010-12-16 12:17:45 -0300574/**
575 * vpif_querycap() - QUERYCAP handler
576 * @file: file ptr
577 * @priv: file handle
578 * @cap: ptr to v4l2_capability structure
579 */
Chaithrika U Se7332e32009-06-09 05:55:37 -0300580static int vpif_querycap(struct file *file, void *priv,
581 struct v4l2_capability *cap)
582{
Muralidharan Karicheri317b2e22009-09-16 14:30:53 -0300583 struct vpif_display_config *config = vpif_dev->platform_data;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300584
Lad, Prabhakar626d533f2012-09-25 11:21:55 -0300585 cap->device_caps = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING;
586 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
Lad, Prabhakar76c4c2b2014-05-16 10:33:22 -0300587 strlcpy(cap->driver, VPIF_DRIVER_NAME, sizeof(cap->driver));
Lad, Prabhakar626d533f2012-09-25 11:21:55 -0300588 snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
589 dev_name(vpif_dev));
Chaithrika U Se7332e32009-06-09 05:55:37 -0300590 strlcpy(cap->card, config->card_name, sizeof(cap->card));
591
592 return 0;
593}
594
595static int vpif_enum_fmt_vid_out(struct file *file, void *priv,
596 struct v4l2_fmtdesc *fmt)
597{
Lad, Prabhakar9cba6532014-05-16 10:33:30 -0300598 if (fmt->index != 0)
Chaithrika U Se7332e32009-06-09 05:55:37 -0300599 return -EINVAL;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300600
601 /* Fill in the information about format */
602 fmt->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
603 strcpy(fmt->description, "YCbCr4:2:2 YC Planar");
604 fmt->pixelformat = V4L2_PIX_FMT_YUV422P;
Lad, Prabhakar9cba6532014-05-16 10:33:30 -0300605 fmt->flags = 0;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300606 return 0;
607}
608
609static int vpif_g_fmt_vid_out(struct file *file, void *priv,
610 struct v4l2_format *fmt)
611{
Lad, Prabhakarfcc22af2014-05-16 10:33:15 -0300612 struct video_device *vdev = video_devdata(file);
613 struct channel_obj *ch = video_get_drvdata(vdev);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300614 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
615
616 /* Check the validity of the buffer type */
617 if (common->fmt.type != fmt->type)
618 return -EINVAL;
619
Mats Randgaardc027e162010-12-16 12:17:44 -0300620 if (vpif_update_resolution(ch))
Hans Verkuil9bfaae22011-01-05 13:35:45 -0300621 return -EINVAL;
622 *fmt = common->fmt;
623 return 0;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300624}
625
Chaithrika U Se7332e32009-06-09 05:55:37 -0300626static int vpif_try_fmt_vid_out(struct file *file, void *priv,
627 struct v4l2_format *fmt)
628{
Lad, Prabhakarfcc22af2014-05-16 10:33:15 -0300629 struct video_device *vdev = video_devdata(file);
630 struct channel_obj *ch = video_get_drvdata(vdev);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300631 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
632 struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300633
Lad, Prabhakar9cba6532014-05-16 10:33:30 -0300634 /*
635 * to supress v4l-compliance warnings silently correct
636 * the pixelformat
637 */
638 if (pixfmt->pixelformat != V4L2_PIX_FMT_YUV422P)
639 pixfmt->pixelformat = common->fmt.fmt.pix.pixelformat;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300640
Lad, Prabhakar9cba6532014-05-16 10:33:30 -0300641 if (vpif_update_resolution(ch))
642 return -EINVAL;
643
644 pixfmt->colorspace = common->fmt.fmt.pix.colorspace;
645 pixfmt->field = common->fmt.fmt.pix.field;
646 pixfmt->bytesperline = common->fmt.fmt.pix.width;
647 pixfmt->width = common->fmt.fmt.pix.width;
648 pixfmt->height = common->fmt.fmt.pix.height;
649 pixfmt->sizeimage = pixfmt->bytesperline * pixfmt->height * 2;
Lad, Prabhakar9cba6532014-05-16 10:33:30 -0300650
651 return 0;
652}
653
654static int vpif_s_fmt_vid_out(struct file *file, void *priv,
655 struct v4l2_format *fmt)
656{
657 struct video_device *vdev = video_devdata(file);
658 struct channel_obj *ch = video_get_drvdata(vdev);
659 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
660 struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
661 int ret;
662
663 if (vb2_is_busy(&common->buffer_queue))
664 return -EBUSY;
665
666 ret = vpif_try_fmt_vid_out(file, priv, fmt);
667 if (ret)
668 return ret;
669
670 /* store the pix format in the channel object */
671 common->fmt.fmt.pix = *pixfmt;
672
673 /* store the format in the channel object */
674 common->fmt = *fmt;
675 return 0;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300676}
677
Hans Verkuil314527a2013-03-15 06:10:40 -0300678static int vpif_s_std(struct file *file, void *priv, v4l2_std_id std_id)
Chaithrika U Se7332e32009-06-09 05:55:37 -0300679{
Lad, Prabhakarf27d0f42014-05-16 10:33:28 -0300680 struct vpif_display_config *config = vpif_dev->platform_data;
Lad, Prabhakarfcc22af2014-05-16 10:33:15 -0300681 struct video_device *vdev = video_devdata(file);
682 struct channel_obj *ch = video_get_drvdata(vdev);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300683 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
Lad, Prabhakarf27d0f42014-05-16 10:33:28 -0300684 struct vpif_display_chan_config *chan_cfg;
685 struct v4l2_output output;
686 int ret;
687
688 if (config->chan_config[ch->channel_id].outputs == NULL)
689 return -ENODATA;
690
691 chan_cfg = &config->chan_config[ch->channel_id];
692 output = chan_cfg->outputs[ch->output_idx].output;
693 if (output.capabilities != V4L2_OUT_CAP_STD)
694 return -ENODATA;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300695
Lad, Prabhakar2f5851b2014-05-16 10:33:21 -0300696 if (vb2_is_busy(&common->buffer_queue))
697 return -EBUSY;
698
Lad, Prabhakarf27d0f42014-05-16 10:33:28 -0300699
Hans Verkuil314527a2013-03-15 06:10:40 -0300700 if (!(std_id & VPIF_V4L2_STD))
Chaithrika U Se7332e32009-06-09 05:55:37 -0300701 return -EINVAL;
702
Chaithrika U Se7332e32009-06-09 05:55:37 -0300703 /* Call encoder subdevice function to set the standard */
Hans Verkuil314527a2013-03-15 06:10:40 -0300704 ch->video.stdid = std_id;
Hans Verkuil0598c172012-09-18 07:18:47 -0300705 memset(&ch->video.dv_timings, 0, sizeof(ch->video.dv_timings));
Chaithrika U Se7332e32009-06-09 05:55:37 -0300706 /* Get the information about the standard */
Hans Verkuil9bfaae22011-01-05 13:35:45 -0300707 if (vpif_update_resolution(ch))
708 return -EINVAL;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300709
Chaithrika U Se7332e32009-06-09 05:55:37 -0300710 common->fmt.fmt.pix.bytesperline = common->fmt.fmt.pix.width;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300711
712 ret = v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 1, video,
Hans Verkuil314527a2013-03-15 06:10:40 -0300713 s_std_output, std_id);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300714 if (ret < 0) {
715 vpif_err("Failed to set output standard\n");
Hans Verkuil9bfaae22011-01-05 13:35:45 -0300716 return ret;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300717 }
718
Laurent Pinchart8774bed2014-04-28 16:53:01 -0300719 ret = v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 1, video,
Hans Verkuil314527a2013-03-15 06:10:40 -0300720 s_std, std_id);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300721 if (ret < 0)
722 vpif_err("Failed to set standard for sub devices\n");
Chaithrika U Se7332e32009-06-09 05:55:37 -0300723 return ret;
724}
725
726static int vpif_g_std(struct file *file, void *priv, v4l2_std_id *std)
727{
Lad, Prabhakarf27d0f42014-05-16 10:33:28 -0300728 struct vpif_display_config *config = vpif_dev->platform_data;
Lad, Prabhakarfcc22af2014-05-16 10:33:15 -0300729 struct video_device *vdev = video_devdata(file);
730 struct channel_obj *ch = video_get_drvdata(vdev);
Lad, Prabhakarf27d0f42014-05-16 10:33:28 -0300731 struct vpif_display_chan_config *chan_cfg;
732 struct v4l2_output output;
733
734 if (config->chan_config[ch->channel_id].outputs == NULL)
735 return -ENODATA;
736
737 chan_cfg = &config->chan_config[ch->channel_id];
738 output = chan_cfg->outputs[ch->output_idx].output;
739 if (output.capabilities != V4L2_OUT_CAP_STD)
740 return -ENODATA;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300741
742 *std = ch->video.stdid;
743 return 0;
744}
745
Chaithrika U Se7332e32009-06-09 05:55:37 -0300746static int vpif_enum_output(struct file *file, void *fh,
747 struct v4l2_output *output)
748{
749
Muralidharan Karicheri317b2e22009-09-16 14:30:53 -0300750 struct vpif_display_config *config = vpif_dev->platform_data;
Lad, Prabhakarfcc22af2014-05-16 10:33:15 -0300751 struct video_device *vdev = video_devdata(file);
752 struct channel_obj *ch = video_get_drvdata(vdev);
Lad, Prabhakar2bd4e582012-09-25 08:11:49 -0300753 struct vpif_display_chan_config *chan_cfg;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300754
Lad, Prabhakar2bd4e582012-09-25 08:11:49 -0300755 chan_cfg = &config->chan_config[ch->channel_id];
756 if (output->index >= chan_cfg->output_count) {
Chaithrika U Se7332e32009-06-09 05:55:37 -0300757 vpif_dbg(1, debug, "Invalid output index\n");
758 return -EINVAL;
759 }
760
Lad, Prabhakar2bd4e582012-09-25 08:11:49 -0300761 *output = chan_cfg->outputs[output->index].output;
762 return 0;
763}
Chaithrika U Se7332e32009-06-09 05:55:37 -0300764
Lad, Prabhakar2bd4e582012-09-25 08:11:49 -0300765/**
766 * vpif_output_to_subdev() - Maps output to sub device
767 * @vpif_cfg - global config ptr
768 * @chan_cfg - channel config ptr
769 * @index - Given output index from application
770 *
771 * lookup the sub device information for a given output index.
772 * we report all the output to application. output table also
773 * has sub device name for the each output
774 */
775static int
776vpif_output_to_subdev(struct vpif_display_config *vpif_cfg,
777 struct vpif_display_chan_config *chan_cfg, int index)
778{
779 struct vpif_subdev_info *subdev_info;
780 const char *subdev_name;
781 int i;
782
783 vpif_dbg(2, debug, "vpif_output_to_subdev\n");
784
785 if (chan_cfg->outputs == NULL)
786 return -1;
787
788 subdev_name = chan_cfg->outputs[index].subdev_name;
789 if (subdev_name == NULL)
790 return -1;
791
792 /* loop through the sub device list to get the sub device info */
793 for (i = 0; i < vpif_cfg->subdev_count; i++) {
794 subdev_info = &vpif_cfg->subdevinfo[i];
795 if (!strcmp(subdev_info->name, subdev_name))
796 return i;
797 }
798 return -1;
799}
800
801/**
802 * vpif_set_output() - Select an output
803 * @vpif_cfg - global config ptr
804 * @ch - channel
805 * @index - Given output index from application
806 *
807 * Select the given output.
808 */
809static int vpif_set_output(struct vpif_display_config *vpif_cfg,
810 struct channel_obj *ch, int index)
811{
812 struct vpif_display_chan_config *chan_cfg =
813 &vpif_cfg->chan_config[ch->channel_id];
Lad, Prabhakar2bd4e582012-09-25 08:11:49 -0300814 struct v4l2_subdev *sd = NULL;
815 u32 input = 0, output = 0;
816 int sd_index;
817 int ret;
818
819 sd_index = vpif_output_to_subdev(vpif_cfg, chan_cfg, index);
Mauro Carvalho Chehaba4f20e22014-08-21 15:46:46 -0500820 if (sd_index >= 0)
Lad, Prabhakar2bd4e582012-09-25 08:11:49 -0300821 sd = vpif_obj.sd[sd_index];
Lad, Prabhakar2bd4e582012-09-25 08:11:49 -0300822
823 if (sd) {
824 input = chan_cfg->outputs[index].input_route;
825 output = chan_cfg->outputs[index].output_route;
826 ret = v4l2_subdev_call(sd, video, s_routing, input, output, 0);
827 if (ret < 0 && ret != -ENOIOCTLCMD) {
828 vpif_err("Failed to set output\n");
829 return ret;
830 }
831
832 }
833 ch->output_idx = index;
834 ch->sd = sd;
835 if (chan_cfg->outputs != NULL)
836 /* update tvnorms from the sub device output info */
Lad, Prabhakare933c6b2015-03-08 18:57:24 -0300837 ch->video_dev.tvnorms = chan_cfg->outputs[index].output.std;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300838 return 0;
839}
840
841static int vpif_s_output(struct file *file, void *priv, unsigned int i)
842{
Lad, Prabhakar2bd4e582012-09-25 08:11:49 -0300843 struct vpif_display_config *config = vpif_dev->platform_data;
Lad, Prabhakarfcc22af2014-05-16 10:33:15 -0300844 struct video_device *vdev = video_devdata(file);
845 struct channel_obj *ch = video_get_drvdata(vdev);
Lad, Prabhakar2bd4e582012-09-25 08:11:49 -0300846 struct vpif_display_chan_config *chan_cfg;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300847 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
Lad, Prabhakar2bd4e582012-09-25 08:11:49 -0300848
Lad, Prabhakar2f5851b2014-05-16 10:33:21 -0300849 if (vb2_is_busy(&common->buffer_queue))
850 return -EBUSY;
851
Lad, Prabhakar2bd4e582012-09-25 08:11:49 -0300852 chan_cfg = &config->chan_config[ch->channel_id];
853
854 if (i >= chan_cfg->output_count)
855 return -EINVAL;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300856
Lad, Prabhakar2bd4e582012-09-25 08:11:49 -0300857 return vpif_set_output(config, ch, i);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300858}
859
860static int vpif_g_output(struct file *file, void *priv, unsigned int *i)
861{
Lad, Prabhakarfcc22af2014-05-16 10:33:15 -0300862 struct video_device *vdev = video_devdata(file);
863 struct channel_obj *ch = video_get_drvdata(vdev);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300864
Hans Verkuil311673e2012-09-20 09:06:23 -0300865 *i = ch->output_idx;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300866
867 return 0;
868}
869
Mats Randgaard40c8bce2010-12-16 12:17:43 -0300870/**
Hans Verkuil0598c172012-09-18 07:18:47 -0300871 * vpif_enum_dv_timings() - ENUM_DV_TIMINGS handler
Mats Randgaard40c8bce2010-12-16 12:17:43 -0300872 * @file: file ptr
873 * @priv: file handle
Hans Verkuil0598c172012-09-18 07:18:47 -0300874 * @timings: input timings
Mats Randgaard40c8bce2010-12-16 12:17:43 -0300875 */
Hans Verkuil0598c172012-09-18 07:18:47 -0300876static int
877vpif_enum_dv_timings(struct file *file, void *priv,
878 struct v4l2_enum_dv_timings *timings)
Mats Randgaard40c8bce2010-12-16 12:17:43 -0300879{
Lad, Prabhakar1093c592014-05-16 10:33:27 -0300880 struct vpif_display_config *config = vpif_dev->platform_data;
Lad, Prabhakarfcc22af2014-05-16 10:33:15 -0300881 struct video_device *vdev = video_devdata(file);
882 struct channel_obj *ch = video_get_drvdata(vdev);
Lad, Prabhakar1093c592014-05-16 10:33:27 -0300883 struct vpif_display_chan_config *chan_cfg;
884 struct v4l2_output output;
Lad, Prabhakar2bd4e582012-09-25 08:11:49 -0300885 int ret;
Mats Randgaard40c8bce2010-12-16 12:17:43 -0300886
Lad, Prabhakar1093c592014-05-16 10:33:27 -0300887 if (config->chan_config[ch->channel_id].outputs == NULL)
888 return -ENODATA;
889
890 chan_cfg = &config->chan_config[ch->channel_id];
891 output = chan_cfg->outputs[ch->output_idx].output;
892 if (output.capabilities != V4L2_OUT_CAP_DV_TIMINGS)
893 return -ENODATA;
894
Laurent Pinchart10d21462014-01-31 09:04:19 -0300895 timings->pad = 0;
896
897 ret = v4l2_subdev_call(ch->sd, pad, enum_dv_timings, timings);
Wei Yongjun63af4af2012-10-30 09:49:38 -0300898 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
Lad, Prabhakar2bd4e582012-09-25 08:11:49 -0300899 return -EINVAL;
900 return ret;
Mats Randgaard40c8bce2010-12-16 12:17:43 -0300901}
902
903/**
Mats Randgaardc027e162010-12-16 12:17:44 -0300904 * vpif_s_dv_timings() - S_DV_TIMINGS handler
905 * @file: file ptr
906 * @priv: file handle
907 * @timings: digital video timings
908 */
909static int vpif_s_dv_timings(struct file *file, void *priv,
910 struct v4l2_dv_timings *timings)
911{
Lad, Prabhakar1093c592014-05-16 10:33:27 -0300912 struct vpif_display_config *config = vpif_dev->platform_data;
Lad, Prabhakarfcc22af2014-05-16 10:33:15 -0300913 struct video_device *vdev = video_devdata(file);
914 struct channel_obj *ch = video_get_drvdata(vdev);
Mats Randgaardc027e162010-12-16 12:17:44 -0300915 struct vpif_params *vpifparams = &ch->vpifparams;
Lad, Prabhakar1093c592014-05-16 10:33:27 -0300916 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
Mats Randgaardc027e162010-12-16 12:17:44 -0300917 struct vpif_channel_config_params *std_info = &vpifparams->std_info;
918 struct video_obj *vid_ch = &ch->video;
Hans Verkuil0598c172012-09-18 07:18:47 -0300919 struct v4l2_bt_timings *bt = &vid_ch->dv_timings.bt;
Lad, Prabhakar1093c592014-05-16 10:33:27 -0300920 struct vpif_display_chan_config *chan_cfg;
921 struct v4l2_output output;
Mats Randgaardc027e162010-12-16 12:17:44 -0300922 int ret;
923
Lad, Prabhakar1093c592014-05-16 10:33:27 -0300924 if (config->chan_config[ch->channel_id].outputs == NULL)
925 return -ENODATA;
926
927 chan_cfg = &config->chan_config[ch->channel_id];
928 output = chan_cfg->outputs[ch->output_idx].output;
929 if (output.capabilities != V4L2_OUT_CAP_DV_TIMINGS)
930 return -ENODATA;
931
932 if (vb2_is_busy(&common->buffer_queue))
933 return -EBUSY;
934
Mats Randgaardc027e162010-12-16 12:17:44 -0300935 if (timings->type != V4L2_DV_BT_656_1120) {
936 vpif_dbg(2, debug, "Timing type not defined\n");
937 return -EINVAL;
938 }
939
940 /* Configure subdevice timings, if any */
Hans Verkuil882084a2012-09-20 09:06:31 -0300941 ret = v4l2_subdev_call(ch->sd, video, s_dv_timings, timings);
Lad, Prabhakar2bd4e582012-09-25 08:11:49 -0300942 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
943 ret = 0;
944 if (ret < 0) {
Mats Randgaardc027e162010-12-16 12:17:44 -0300945 vpif_dbg(2, debug, "Error setting custom DV timings\n");
946 return ret;
947 }
948
949 if (!(timings->bt.width && timings->bt.height &&
950 (timings->bt.hbackporch ||
951 timings->bt.hfrontporch ||
952 timings->bt.hsync) &&
953 timings->bt.vfrontporch &&
954 (timings->bt.vbackporch ||
955 timings->bt.vsync))) {
956 vpif_dbg(2, debug, "Timings for width, height, "
957 "horizontal back porch, horizontal sync, "
958 "horizontal front porch, vertical back porch, "
959 "vertical sync and vertical back porch "
960 "must be defined\n");
961 return -EINVAL;
962 }
963
Hans Verkuil0598c172012-09-18 07:18:47 -0300964 vid_ch->dv_timings = *timings;
Mats Randgaardc027e162010-12-16 12:17:44 -0300965
966 /* Configure video port timings */
967
Hans Verkuile3655262013-07-29 08:41:00 -0300968 std_info->eav2sav = V4L2_DV_BT_BLANKING_WIDTH(bt) - 8;
Mats Randgaardc027e162010-12-16 12:17:44 -0300969 std_info->sav2eav = bt->width;
970
971 std_info->l1 = 1;
972 std_info->l3 = bt->vsync + bt->vbackporch + 1;
973
Hans Verkuile3655262013-07-29 08:41:00 -0300974 std_info->vsize = V4L2_DV_BT_FRAME_HEIGHT(bt);
Mats Randgaardc027e162010-12-16 12:17:44 -0300975 if (bt->interlaced) {
976 if (bt->il_vbackporch || bt->il_vfrontporch || bt->il_vsync) {
Mats Randgaardc027e162010-12-16 12:17:44 -0300977 std_info->l5 = std_info->vsize/2 -
978 (bt->vfrontporch - 1);
979 std_info->l7 = std_info->vsize/2 + 1;
980 std_info->l9 = std_info->l7 + bt->il_vsync +
981 bt->il_vbackporch + 1;
982 std_info->l11 = std_info->vsize -
983 (bt->il_vfrontporch - 1);
984 } else {
985 vpif_dbg(2, debug, "Required timing values for "
986 "interlaced BT format missing\n");
987 return -EINVAL;
988 }
989 } else {
Mats Randgaardc027e162010-12-16 12:17:44 -0300990 std_info->l5 = std_info->vsize - (bt->vfrontporch - 1);
991 }
992 strncpy(std_info->name, "Custom timings BT656/1120",
993 VPIF_MAX_NAME);
994 std_info->width = bt->width;
995 std_info->height = bt->height;
996 std_info->frm_fmt = bt->interlaced ? 0 : 1;
997 std_info->ycmux_mode = 0;
998 std_info->capture_format = 0;
999 std_info->vbi_supported = 0;
1000 std_info->hd_sd = 1;
1001 std_info->stdid = 0;
Mats Randgaardc027e162010-12-16 12:17:44 -03001002 vid_ch->stdid = 0;
Mats Randgaardc027e162010-12-16 12:17:44 -03001003
1004 return 0;
1005}
1006
1007/**
1008 * vpif_g_dv_timings() - G_DV_TIMINGS handler
1009 * @file: file ptr
1010 * @priv: file handle
1011 * @timings: digital video timings
1012 */
1013static int vpif_g_dv_timings(struct file *file, void *priv,
1014 struct v4l2_dv_timings *timings)
1015{
Lad, Prabhakar1093c592014-05-16 10:33:27 -03001016 struct vpif_display_config *config = vpif_dev->platform_data;
Lad, Prabhakarfcc22af2014-05-16 10:33:15 -03001017 struct video_device *vdev = video_devdata(file);
1018 struct channel_obj *ch = video_get_drvdata(vdev);
Lad, Prabhakar1093c592014-05-16 10:33:27 -03001019 struct vpif_display_chan_config *chan_cfg;
Mats Randgaardc027e162010-12-16 12:17:44 -03001020 struct video_obj *vid_ch = &ch->video;
Lad, Prabhakar1093c592014-05-16 10:33:27 -03001021 struct v4l2_output output;
1022
1023 if (config->chan_config[ch->channel_id].outputs == NULL)
1024 goto error;
1025
1026 chan_cfg = &config->chan_config[ch->channel_id];
1027 output = chan_cfg->outputs[ch->output_idx].output;
1028
1029 if (output.capabilities != V4L2_OUT_CAP_DV_TIMINGS)
1030 goto error;
Mats Randgaardc027e162010-12-16 12:17:44 -03001031
Hans Verkuil0598c172012-09-18 07:18:47 -03001032 *timings = vid_ch->dv_timings;
Mats Randgaardc027e162010-12-16 12:17:44 -03001033
1034 return 0;
Lad, Prabhakar1093c592014-05-16 10:33:27 -03001035error:
1036 return -ENODATA;
Mats Randgaardc027e162010-12-16 12:17:44 -03001037}
Mats Randgaard7036d6a2010-12-16 12:17:41 -03001038
1039/*
Mats Randgaard7036d6a2010-12-16 12:17:41 -03001040 * vpif_log_status() - Status information
1041 * @file: file ptr
1042 * @priv: file handle
1043 *
1044 * Returns zero.
1045 */
1046static int vpif_log_status(struct file *filep, void *priv)
1047{
1048 /* status for sub devices */
1049 v4l2_device_call_all(&vpif_obj.v4l2_dev, 0, core, log_status);
1050
1051 return 0;
1052}
1053
Chaithrika U Se7332e32009-06-09 05:55:37 -03001054/* vpif display ioctl operations */
1055static const struct v4l2_ioctl_ops vpif_ioctl_ops = {
Lad, Prabhakardcce8662014-05-16 10:33:25 -03001056 .vidioc_querycap = vpif_querycap,
Chaithrika U Se7332e32009-06-09 05:55:37 -03001057 .vidioc_enum_fmt_vid_out = vpif_enum_fmt_vid_out,
Lad, Prabhakardcce8662014-05-16 10:33:25 -03001058 .vidioc_g_fmt_vid_out = vpif_g_fmt_vid_out,
1059 .vidioc_s_fmt_vid_out = vpif_s_fmt_vid_out,
1060 .vidioc_try_fmt_vid_out = vpif_try_fmt_vid_out,
Lad, Prabhakarb92cde12014-05-16 10:33:16 -03001061
1062 .vidioc_reqbufs = vb2_ioctl_reqbufs,
1063 .vidioc_create_bufs = vb2_ioctl_create_bufs,
1064 .vidioc_querybuf = vb2_ioctl_querybuf,
1065 .vidioc_qbuf = vb2_ioctl_qbuf,
1066 .vidioc_dqbuf = vb2_ioctl_dqbuf,
1067 .vidioc_expbuf = vb2_ioctl_expbuf,
1068 .vidioc_streamon = vb2_ioctl_streamon,
1069 .vidioc_streamoff = vb2_ioctl_streamoff,
1070
Lad, Prabhakardcce8662014-05-16 10:33:25 -03001071 .vidioc_s_std = vpif_s_std,
Chaithrika U Se7332e32009-06-09 05:55:37 -03001072 .vidioc_g_std = vpif_g_std,
Lad, Prabhakardcce8662014-05-16 10:33:25 -03001073
Chaithrika U Se7332e32009-06-09 05:55:37 -03001074 .vidioc_enum_output = vpif_enum_output,
1075 .vidioc_s_output = vpif_s_output,
1076 .vidioc_g_output = vpif_g_output,
Lad, Prabhakardcce8662014-05-16 10:33:25 -03001077
1078 .vidioc_enum_dv_timings = vpif_enum_dv_timings,
1079 .vidioc_s_dv_timings = vpif_s_dv_timings,
1080 .vidioc_g_dv_timings = vpif_g_dv_timings,
1081
Mats Randgaard7036d6a2010-12-16 12:17:41 -03001082 .vidioc_log_status = vpif_log_status,
Chaithrika U Se7332e32009-06-09 05:55:37 -03001083};
1084
1085static const struct v4l2_file_operations vpif_fops = {
1086 .owner = THIS_MODULE,
Lad, Prabhakarfcc22af2014-05-16 10:33:15 -03001087 .open = v4l2_fh_open,
1088 .release = vb2_fop_release,
Hans Verkuil9bfaae22011-01-05 13:35:45 -03001089 .unlocked_ioctl = video_ioctl2,
Lad, Prabhakar4be21532014-05-16 10:33:14 -03001090 .mmap = vb2_fop_mmap,
1091 .poll = vb2_fop_poll
Chaithrika U Se7332e32009-06-09 05:55:37 -03001092};
1093
Chaithrika U Se7332e32009-06-09 05:55:37 -03001094/*Configure the channels, buffer sizei, request irq */
1095static int initialize_vpif(void)
1096{
1097 int free_channel_objects_index;
Lad, Prabhakard5b94b92014-05-16 10:33:23 -03001098 int err, i, j;
Chaithrika U Se7332e32009-06-09 05:55:37 -03001099
1100 /* Allocate memory for six channel objects */
1101 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1102 vpif_obj.dev[i] =
Mats Randgaard1f8766b2010-08-30 10:30:37 -03001103 kzalloc(sizeof(struct channel_obj), GFP_KERNEL);
Chaithrika U Se7332e32009-06-09 05:55:37 -03001104 /* If memory allocation fails, return error */
1105 if (!vpif_obj.dev[i]) {
1106 free_channel_objects_index = i;
1107 err = -ENOMEM;
1108 goto vpif_init_free_channel_objects;
1109 }
1110 }
1111
Chaithrika U Se7332e32009-06-09 05:55:37 -03001112 return 0;
1113
1114vpif_init_free_channel_objects:
1115 for (j = 0; j < free_channel_objects_index; j++)
1116 kfree(vpif_obj.dev[j]);
1117 return err;
1118}
1119
Lad, Prabhakar4b8a5312013-06-25 11:17:35 -03001120static int vpif_async_bound(struct v4l2_async_notifier *notifier,
1121 struct v4l2_subdev *subdev,
1122 struct v4l2_async_subdev *asd)
1123{
1124 int i;
1125
1126 for (i = 0; i < vpif_obj.config->subdev_count; i++)
1127 if (!strcmp(vpif_obj.config->subdevinfo[i].name,
1128 subdev->name)) {
1129 vpif_obj.sd[i] = subdev;
1130 vpif_obj.sd[i]->grp_id = 1 << i;
1131 return 0;
1132 }
1133
1134 return -EINVAL;
1135}
1136
1137static int vpif_probe_complete(void)
1138{
1139 struct common_obj *common;
Lad, Prabhakar4be21532014-05-16 10:33:14 -03001140 struct video_device *vdev;
Lad, Prabhakar4b8a5312013-06-25 11:17:35 -03001141 struct channel_obj *ch;
Lad, Prabhakara2b235c2014-05-16 10:33:06 -03001142 struct vb2_queue *q;
Lad, Prabhakar4b8a5312013-06-25 11:17:35 -03001143 int j, err, k;
1144
1145 for (j = 0; j < VPIF_DISPLAY_MAX_DEVICES; j++) {
1146 ch = vpif_obj.dev[j];
1147 /* Initialize field of the channel objects */
Lad, Prabhakar4b8a5312013-06-25 11:17:35 -03001148 for (k = 0; k < VPIF_NUMOBJECTS; k++) {
Lad, Prabhakar4b8a5312013-06-25 11:17:35 -03001149 common = &ch->common[k];
Lad, Prabhakar4b8a5312013-06-25 11:17:35 -03001150 spin_lock_init(&common->irqlock);
1151 mutex_init(&common->lock);
Lad, Prabhakar4b8a5312013-06-25 11:17:35 -03001152 common->set_addr = NULL;
1153 common->ytop_off = 0;
1154 common->ybtm_off = 0;
1155 common->ctop_off = 0;
1156 common->cbtm_off = 0;
1157 common->cur_frm = NULL;
1158 common->next_frm = NULL;
1159 memset(&common->fmt, 0, sizeof(common->fmt));
Lad, Prabhakar4b8a5312013-06-25 11:17:35 -03001160 }
1161 ch->initialized = 0;
1162 if (vpif_obj.config->subdev_count)
1163 ch->sd = vpif_obj.sd[0];
1164 ch->channel_id = j;
Lad, Prabhakar4b8a5312013-06-25 11:17:35 -03001165
1166 memset(&ch->vpifparams, 0, sizeof(ch->vpifparams));
1167
Lad, Prabhakar4b8a5312013-06-25 11:17:35 -03001168 ch->common[VPIF_VIDEO_INDEX].fmt.type =
1169 V4L2_BUF_TYPE_VIDEO_OUTPUT;
Lad, Prabhakar4b8a5312013-06-25 11:17:35 -03001170
1171 /* select output 0 */
1172 err = vpif_set_output(vpif_obj.config, ch, 0);
1173 if (err)
1174 goto probe_out;
1175
Lad, Prabhakar9cba6532014-05-16 10:33:30 -03001176 /* set initial format */
1177 ch->video.stdid = V4L2_STD_525_60;
1178 memset(&ch->video.dv_timings, 0, sizeof(ch->video.dv_timings));
1179 vpif_update_resolution(ch);
1180
Lad, Prabhakara2b235c2014-05-16 10:33:06 -03001181 /* Initialize vb2 queue */
1182 q = &common->buffer_queue;
1183 q->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
1184 q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
1185 q->drv_priv = ch;
1186 q->ops = &video_qops;
1187 q->mem_ops = &vb2_dma_contig_memops;
1188 q->buf_struct_size = sizeof(struct vpif_disp_buffer);
1189 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1190 q->min_buffers_needed = 1;
Lad, Prabhakard10ed5c2014-05-16 10:33:08 -03001191 q->lock = &common->lock;
Lad, Prabhakara2b235c2014-05-16 10:33:06 -03001192 err = vb2_queue_init(q);
1193 if (err) {
1194 vpif_err("vpif_display: vb2_queue_init() failed\n");
1195 goto probe_out;
1196 }
1197
1198 common->alloc_ctx = vb2_dma_contig_init_ctx(vpif_dev);
1199 if (IS_ERR(common->alloc_ctx)) {
1200 vpif_err("Failed to get the context\n");
1201 err = PTR_ERR(common->alloc_ctx);
1202 goto probe_out;
1203 }
1204
1205 INIT_LIST_HEAD(&common->dma_queue);
1206
Lad, Prabhakar4b8a5312013-06-25 11:17:35 -03001207 /* register video device */
Mauro Carvalho Chehab212bdba2014-08-22 06:38:14 -05001208 vpif_dbg(1, debug, "channel=%p,channel->video_dev=%p\n",
1209 ch, &ch->video_dev);
Lad, Prabhakar4b8a5312013-06-25 11:17:35 -03001210
Lad, Prabhakar76c4c2b2014-05-16 10:33:22 -03001211 /* Initialize the video_device structure */
Lad, Prabhakare933c6b2015-03-08 18:57:24 -03001212 vdev = &ch->video_dev;
Lad, Prabhakar76c4c2b2014-05-16 10:33:22 -03001213 strlcpy(vdev->name, VPIF_DRIVER_NAME, sizeof(vdev->name));
Lad, Prabhakare933c6b2015-03-08 18:57:24 -03001214 vdev->release = video_device_release_empty;
Lad, Prabhakar76c4c2b2014-05-16 10:33:22 -03001215 vdev->fops = &vpif_fops;
1216 vdev->ioctl_ops = &vpif_ioctl_ops;
1217 vdev->v4l2_dev = &vpif_obj.v4l2_dev;
1218 vdev->vfl_dir = VFL_DIR_TX;
Lad, Prabhakar4be21532014-05-16 10:33:14 -03001219 vdev->queue = q;
Lad, Prabhakarfcc22af2014-05-16 10:33:15 -03001220 vdev->lock = &common->lock;
Lad, Prabhakare933c6b2015-03-08 18:57:24 -03001221 video_set_drvdata(&ch->video_dev, ch);
Lad, Prabhakar4be21532014-05-16 10:33:14 -03001222 err = video_register_device(vdev, VFL_TYPE_GRABBER,
1223 (j ? 3 : 2));
Lad, Prabhakar4b8a5312013-06-25 11:17:35 -03001224 if (err < 0)
1225 goto probe_out;
1226 }
1227
1228 return 0;
1229
1230probe_out:
1231 for (k = 0; k < j; k++) {
1232 ch = vpif_obj.dev[k];
Lad, Prabhakara2b235c2014-05-16 10:33:06 -03001233 common = &ch->common[k];
1234 vb2_dma_contig_cleanup_ctx(common->alloc_ctx);
Lad, Prabhakare933c6b2015-03-08 18:57:24 -03001235 video_unregister_device(&ch->video_dev);
Lad, Prabhakar4b8a5312013-06-25 11:17:35 -03001236 }
1237 return err;
1238}
1239
1240static int vpif_async_complete(struct v4l2_async_notifier *notifier)
1241{
1242 return vpif_probe_complete();
1243}
1244
Chaithrika U Se7332e32009-06-09 05:55:37 -03001245/*
1246 * vpif_probe: This function creates device entries by register itself to the
1247 * V4L2 driver and initializes fields of each channel objects
1248 */
1249static __init int vpif_probe(struct platform_device *pdev)
1250{
Muralidharan Karicheri317b2e22009-09-16 14:30:53 -03001251 struct vpif_subdev_info *subdevdata;
Chaithrika U Se7332e32009-06-09 05:55:37 -03001252 struct i2c_adapter *i2c_adap;
Chaithrika U Se7332e32009-06-09 05:55:37 -03001253 struct resource *res;
1254 int subdev_count;
Lad, Prabhakare933c6b2015-03-08 18:57:24 -03001255 int res_idx = 0;
1256 int i, err;
Chaithrika U Se7332e32009-06-09 05:55:37 -03001257
1258 vpif_dev = &pdev->dev;
Muralidharan Karicheri317b2e22009-09-16 14:30:53 -03001259 err = initialize_vpif();
1260
1261 if (err) {
1262 v4l2_err(vpif_dev->driver, "Error initializing vpif\n");
1263 return err;
Chaithrika U Se7332e32009-06-09 05:55:37 -03001264 }
1265
Chaithrika U Se7332e32009-06-09 05:55:37 -03001266 err = v4l2_device_register(vpif_dev, &vpif_obj.v4l2_dev);
1267 if (err) {
1268 v4l2_err(vpif_dev->driver, "Error registering v4l2 device\n");
1269 return err;
1270 }
1271
Hans Verkuil01b1d9752012-09-20 09:06:28 -03001272 while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, res_idx))) {
Lad, Prabhakar379d2cf2013-06-17 11:20:51 -03001273 err = devm_request_irq(&pdev->dev, res->start, vpif_channel_isr,
Lad, Prabhakar76c4c2b2014-05-16 10:33:22 -03001274 IRQF_SHARED, VPIF_DRIVER_NAME,
Lad, Prabhakar379d2cf2013-06-17 11:20:51 -03001275 (void *)(&vpif_obj.dev[res_idx]->
1276 channel_id));
1277 if (err) {
1278 err = -EINVAL;
1279 vpif_err("VPIF IRQ request failed\n");
1280 goto vpif_unregister;
Chaithrika U Se7332e32009-06-09 05:55:37 -03001281 }
Hans Verkuil01b1d9752012-09-20 09:06:28 -03001282 res_idx++;
Chaithrika U Se7332e32009-06-09 05:55:37 -03001283 }
1284
Lad, Prabhakar4b8a5312013-06-25 11:17:35 -03001285 vpif_obj.config = pdev->dev.platform_data;
1286 subdev_count = vpif_obj.config->subdev_count;
1287 subdevdata = vpif_obj.config->subdevinfo;
Hans Verkuile6067f82012-09-20 09:06:27 -03001288 vpif_obj.sd = kzalloc(sizeof(struct v4l2_subdev *) * subdev_count,
1289 GFP_KERNEL);
1290 if (vpif_obj.sd == NULL) {
1291 vpif_err("unable to allocate memory for subdevice pointers\n");
1292 err = -ENOMEM;
Lad, Prabhakare933c6b2015-03-08 18:57:24 -03001293 goto vpif_unregister;
Hans Verkuile6067f82012-09-20 09:06:27 -03001294 }
1295
Lad, Prabhakar4b8a5312013-06-25 11:17:35 -03001296 if (!vpif_obj.config->asd_sizes) {
1297 i2c_adap = i2c_get_adapter(1);
1298 for (i = 0; i < subdev_count; i++) {
1299 vpif_obj.sd[i] =
1300 v4l2_i2c_new_subdev_board(&vpif_obj.v4l2_dev,
1301 i2c_adap,
1302 &subdevdata[i].
1303 board_info,
1304 NULL);
1305 if (!vpif_obj.sd[i]) {
1306 vpif_err("Error registering v4l2 subdevice\n");
Wei Yongjun9d3e9762013-08-22 22:59:44 -03001307 err = -ENODEV;
Lad, Prabhakar4b8a5312013-06-25 11:17:35 -03001308 goto probe_subdev_out;
1309 }
1310
1311 if (vpif_obj.sd[i])
1312 vpif_obj.sd[i]->grp_id = 1 << i;
1313 }
1314 vpif_probe_complete();
1315 } else {
Sylwester Nawrockie8419d02013-07-19 12:31:10 -03001316 vpif_obj.notifier.subdevs = vpif_obj.config->asd;
Lad, Prabhakar4b8a5312013-06-25 11:17:35 -03001317 vpif_obj.notifier.num_subdevs = vpif_obj.config->asd_sizes[0];
1318 vpif_obj.notifier.bound = vpif_async_bound;
1319 vpif_obj.notifier.complete = vpif_async_complete;
1320 err = v4l2_async_notifier_register(&vpif_obj.v4l2_dev,
1321 &vpif_obj.notifier);
1322 if (err) {
1323 vpif_err("Error registering async notifier\n");
1324 err = -EINVAL;
Hans Verkuile6067f82012-09-20 09:06:27 -03001325 goto probe_subdev_out;
1326 }
Hans Verkuile6067f82012-09-20 09:06:27 -03001327 }
1328
Chaithrika U Se7332e32009-06-09 05:55:37 -03001329 return 0;
1330
Hans Verkuile6067f82012-09-20 09:06:27 -03001331probe_subdev_out:
1332 kfree(vpif_obj.sd);
Lad, Prabhakar9c63e012013-06-17 11:20:50 -03001333vpif_unregister:
Chaithrika U Se7332e32009-06-09 05:55:37 -03001334 v4l2_device_unregister(&vpif_obj.v4l2_dev);
Chaithrika U Se7332e32009-06-09 05:55:37 -03001335
1336 return err;
1337}
1338
1339/*
1340 * vpif_remove: It un-register channels from V4L2 driver
1341 */
1342static int vpif_remove(struct platform_device *device)
1343{
Lad, Prabhakara2b235c2014-05-16 10:33:06 -03001344 struct common_obj *common;
Chaithrika U Se7332e32009-06-09 05:55:37 -03001345 struct channel_obj *ch;
Lad, Prabhakar9c63e012013-06-17 11:20:50 -03001346 int i;
Chaithrika U Se7332e32009-06-09 05:55:37 -03001347
1348 v4l2_device_unregister(&vpif_obj.v4l2_dev);
1349
Lad, Prabhakar0b361582013-06-17 11:20:48 -03001350 kfree(vpif_obj.sd);
Chaithrika U Se7332e32009-06-09 05:55:37 -03001351 /* un-register device */
1352 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1353 /* Get the pointer to the channel object */
1354 ch = vpif_obj.dev[i];
Prabhakar Lade9b58722014-07-18 13:31:51 -03001355 common = &ch->common[VPIF_VIDEO_INDEX];
Lad, Prabhakara2b235c2014-05-16 10:33:06 -03001356 vb2_dma_contig_cleanup_ctx(common->alloc_ctx);
Chaithrika U Se7332e32009-06-09 05:55:37 -03001357 /* Unregister video device */
Lad, Prabhakare933c6b2015-03-08 18:57:24 -03001358 video_unregister_device(&ch->video_dev);
Lad, Prabhakar0b361582013-06-17 11:20:48 -03001359 kfree(vpif_obj.dev[i]);
Chaithrika U Se7332e32009-06-09 05:55:37 -03001360 }
1361
1362 return 0;
1363}
1364
Lad, Prabhakarba1902e2014-05-16 10:33:26 -03001365#ifdef CONFIG_PM_SLEEP
Manjunath Hadlie9530da2012-04-13 04:50:35 -03001366static int vpif_suspend(struct device *dev)
1367{
1368 struct common_obj *common;
1369 struct channel_obj *ch;
1370 int i;
1371
1372 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1373 /* Get the pointer to the channel object */
1374 ch = vpif_obj.dev[i];
1375 common = &ch->common[VPIF_VIDEO_INDEX];
Lad, Prabhakarba1902e2014-05-16 10:33:26 -03001376
Prabhakar Lad81578922014-09-06 12:26:50 -03001377 if (!vb2_start_streaming_called(&common->buffer_queue))
Lad, Prabhakarba1902e2014-05-16 10:33:26 -03001378 continue;
1379
Manjunath Hadlie9530da2012-04-13 04:50:35 -03001380 mutex_lock(&common->lock);
Lad, Prabhakarba1902e2014-05-16 10:33:26 -03001381 /* Disable channel */
1382 if (ch->channel_id == VPIF_CHANNEL2_VIDEO) {
1383 enable_channel2(0);
1384 channel2_intr_enable(0);
1385 }
1386 if (ch->channel_id == VPIF_CHANNEL3_VIDEO ||
1387 ycmux_mode == 2) {
1388 enable_channel3(0);
1389 channel3_intr_enable(0);
Manjunath Hadlie9530da2012-04-13 04:50:35 -03001390 }
1391 mutex_unlock(&common->lock);
1392 }
1393
1394 return 0;
1395}
1396
1397static int vpif_resume(struct device *dev)
1398{
1399
1400 struct common_obj *common;
1401 struct channel_obj *ch;
1402 int i;
1403
1404 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1405 /* Get the pointer to the channel object */
1406 ch = vpif_obj.dev[i];
1407 common = &ch->common[VPIF_VIDEO_INDEX];
Lad, Prabhakarba1902e2014-05-16 10:33:26 -03001408
Prabhakar Lad81578922014-09-06 12:26:50 -03001409 if (!vb2_start_streaming_called(&common->buffer_queue))
Lad, Prabhakarba1902e2014-05-16 10:33:26 -03001410 continue;
1411
Manjunath Hadlie9530da2012-04-13 04:50:35 -03001412 mutex_lock(&common->lock);
Lad, Prabhakarba1902e2014-05-16 10:33:26 -03001413 /* Enable channel */
1414 if (ch->channel_id == VPIF_CHANNEL2_VIDEO) {
1415 enable_channel2(1);
1416 channel2_intr_enable(1);
1417 }
1418 if (ch->channel_id == VPIF_CHANNEL3_VIDEO ||
1419 ycmux_mode == 2) {
1420 enable_channel3(1);
1421 channel3_intr_enable(1);
Manjunath Hadlie9530da2012-04-13 04:50:35 -03001422 }
1423 mutex_unlock(&common->lock);
1424 }
1425
1426 return 0;
1427}
1428
Manjunath Hadlie9530da2012-04-13 04:50:35 -03001429#endif
1430
Lad, Prabhakarba1902e2014-05-16 10:33:26 -03001431static SIMPLE_DEV_PM_OPS(vpif_pm_ops, vpif_suspend, vpif_resume);
1432
Mats Randgaardffa1b392010-08-30 10:30:36 -03001433static __refdata struct platform_driver vpif_driver = {
Chaithrika U Se7332e32009-06-09 05:55:37 -03001434 .driver = {
Lad, Prabhakar76c4c2b2014-05-16 10:33:22 -03001435 .name = VPIF_DRIVER_NAME,
Lad, Prabhakarba1902e2014-05-16 10:33:26 -03001436 .pm = &vpif_pm_ops,
Chaithrika U Se7332e32009-06-09 05:55:37 -03001437 },
1438 .probe = vpif_probe,
1439 .remove = vpif_remove,
1440};
1441
Lad, Prabhakarb8d067b2013-06-17 11:20:49 -03001442module_platform_driver(vpif_driver);