blob: 6c6bd6b47994e6e4e19ca61980985f26b7e99060 [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
Lad, Prabhakar58334b02014-05-16 10:33:13 -030056static inline struct vpif_disp_buffer *to_vpif_buffer(struct vb2_buffer *vb)
57{
58 return container_of(vb, struct vpif_disp_buffer, vb);
59}
60
Lad, Prabhakar27cdf9b2014-05-16 10:33:11 -030061/**
62 * vpif_buffer_prepare : callback function for buffer prepare
63 * @vb: ptr to vb2_buffer
64 *
65 * This is the callback function for buffer prepare when vb2_qbuf()
66 * function is called. The buffer is prepared and user space virtual address
67 * or user address is converted into physical address
Chaithrika U Se7332e32009-06-09 05:55:37 -030068 */
Lad, Prabhakar2401dd22012-06-28 09:28:36 -030069static int vpif_buffer_prepare(struct vb2_buffer *vb)
Chaithrika U Se7332e32009-06-09 05:55:37 -030070{
Lad, Prabhakar27cdf9b2014-05-16 10:33:11 -030071 struct channel_obj *ch = vb2_get_drv_priv(vb->vb2_queue);
Chaithrika U Se7332e32009-06-09 05:55:37 -030072 struct common_obj *common;
Chaithrika U Se7332e32009-06-09 05:55:37 -030073
Lad, Prabhakara2b235c2014-05-16 10:33:06 -030074 common = &ch->common[VPIF_VIDEO_INDEX];
Chaithrika U Se7332e32009-06-09 05:55:37 -030075
Lad, Prabhakar27cdf9b2014-05-16 10:33:11 -030076 vb2_set_plane_payload(vb, 0, common->fmt.fmt.pix.sizeimage);
77 if (vb2_get_plane_payload(vb, 0) > vb2_plane_size(vb, 0))
78 return -EINVAL;
79
80 vb->v4l2_buf.field = common->fmt.fmt.pix.field;
81
82 if (vb->vb2_queue->type != V4L2_BUF_TYPE_SLICED_VBI_OUTPUT) {
83 unsigned long addr = vb2_dma_contig_plane_dma_addr(vb, 0);
84
85 if (!ISALIGNED(addr + common->ytop_off) ||
Lad, Prabhakar2401dd22012-06-28 09:28:36 -030086 !ISALIGNED(addr + common->ybtm_off) ||
87 !ISALIGNED(addr + common->ctop_off) ||
Lad, Prabhakar27cdf9b2014-05-16 10:33:11 -030088 !ISALIGNED(addr + common->cbtm_off)) {
89 vpif_err("buffer offset not aligned to 8 bytes\n");
90 return -EINVAL;
Chaithrika U Se7332e32009-06-09 05:55:37 -030091 }
Chaithrika U Se7332e32009-06-09 05:55:37 -030092 }
Chaithrika U Se7332e32009-06-09 05:55:37 -030093
Lad, Prabhakar27cdf9b2014-05-16 10:33:11 -030094 return 0;
Chaithrika U Se7332e32009-06-09 05:55:37 -030095}
96
Lad, Prabhakar172392a2014-05-16 10:33:12 -030097/**
98 * vpif_buffer_queue_setup : Callback function for buffer setup.
99 * @vq: vb2_queue ptr
100 * @fmt: v4l2 format
101 * @nbuffers: ptr to number of buffers requested by application
102 * @nplanes:: contains number of distinct video planes needed to hold a frame
103 * @sizes[]: contains the size (in bytes) of each plane.
104 * @alloc_ctxs: ptr to allocation context
105 *
106 * This callback function is called when reqbuf() is called to adjust
107 * the buffer count and buffer size
Chaithrika U Se7332e32009-06-09 05:55:37 -0300108 */
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300109static int vpif_buffer_queue_setup(struct vb2_queue *vq,
110 const struct v4l2_format *fmt,
111 unsigned int *nbuffers, unsigned int *nplanes,
112 unsigned int sizes[], void *alloc_ctxs[])
Chaithrika U Se7332e32009-06-09 05:55:37 -0300113{
Lad, Prabhakara2b235c2014-05-16 10:33:06 -0300114 struct channel_obj *ch = vb2_get_drv_priv(vq);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300115 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
116
Lad, Prabhakar172392a2014-05-16 10:33:12 -0300117 if (fmt && fmt->fmt.pix.sizeimage < common->fmt.fmt.pix.sizeimage)
118 return -EINVAL;
Manjunath Hadlifc613d42012-04-13 04:49:10 -0300119
Lad, Prabhakar172392a2014-05-16 10:33:12 -0300120 if (vq->num_buffers + *nbuffers < 3)
121 *nbuffers = 3 - vq->num_buffers;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300122
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300123 *nplanes = 1;
Lad, Prabhakar172392a2014-05-16 10:33:12 -0300124 sizes[0] = fmt ? fmt->fmt.pix.sizeimage : common->fmt.fmt.pix.sizeimage;
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300125 alloc_ctxs[0] = common->alloc_ctx;
Lad, Prabhakar172392a2014-05-16 10:33:12 -0300126
127 /* Calculate the offset for Y and C data in the buffer */
128 vpif_calculate_offsets(ch);
129
Chaithrika U Se7332e32009-06-09 05:55:37 -0300130 return 0;
131}
132
Lad, Prabhakar58334b02014-05-16 10:33:13 -0300133/**
134 * vpif_buffer_queue : Callback function to add buffer to DMA queue
135 * @vb: ptr to vb2_buffer
136 *
137 * This callback fucntion queues the buffer to DMA engine
Chaithrika U Se7332e32009-06-09 05:55:37 -0300138 */
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300139static void vpif_buffer_queue(struct vb2_buffer *vb)
Chaithrika U Se7332e32009-06-09 05:55:37 -0300140{
Lad, Prabhakar58334b02014-05-16 10:33:13 -0300141 struct vpif_disp_buffer *buf = to_vpif_buffer(vb);
Lad, Prabhakara2b235c2014-05-16 10:33:06 -0300142 struct channel_obj *ch = vb2_get_drv_priv(vb->vb2_queue);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300143 struct common_obj *common;
Hans Verkuilc4697d72012-11-16 12:03:07 -0300144 unsigned long flags;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300145
146 common = &ch->common[VPIF_VIDEO_INDEX];
147
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300148 /* add the buffer to the DMA queue */
Hans Verkuilc4697d72012-11-16 12:03:07 -0300149 spin_lock_irqsave(&common->irqlock, flags);
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300150 list_add_tail(&buf->list, &common->dma_queue);
Hans Verkuilc4697d72012-11-16 12:03:07 -0300151 spin_unlock_irqrestore(&common->irqlock, flags);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300152}
153
Lad, Prabhakar58334b02014-05-16 10:33:13 -0300154/**
155 * vpif_start_streaming : Starts the DMA engine for streaming
156 * @vb: ptr to vb2_buffer
157 * @count: number of buffers
158 */
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300159static int vpif_start_streaming(struct vb2_queue *vq, unsigned int count)
160{
161 struct vpif_display_config *vpif_config_data =
162 vpif_dev->platform_data;
Lad, Prabhakara2b235c2014-05-16 10:33:06 -0300163 struct channel_obj *ch = vb2_get_drv_priv(vq);
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300164 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
165 struct vpif_params *vpif = &ch->vpifparams;
Lad, Prabhakar54a445a42014-05-16 10:33:09 -0300166 struct vpif_disp_buffer *buf, *tmp;
167 unsigned long addr, flags;
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300168 int ret;
169
Hans Verkuilc4697d72012-11-16 12:03:07 -0300170 spin_lock_irqsave(&common->irqlock, flags);
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300171
Lad, Prabhakar2f5851b2014-05-16 10:33:21 -0300172 /* Initialize field_id */
Lad, Prabhakar54a445a42014-05-16 10:33:09 -0300173 ch->field_id = 0;
Lad, Prabhakar54a445a42014-05-16 10:33:09 -0300174
Lad, Prabhakar54a445a42014-05-16 10:33:09 -0300175 /* clock settings */
176 if (vpif_config_data->set_clock) {
177 ret = vpif_config_data->set_clock(ch->vpifparams.std_info.
178 ycmux_mode, ch->vpifparams.std_info.hd_sd);
179 if (ret < 0) {
180 vpif_err("can't set clock\n");
181 goto err;
182 }
183 }
184
185 /* set the parameters and addresses */
186 ret = vpif_set_video_params(vpif, ch->channel_id + 2);
187 if (ret < 0)
188 goto err;
189
Lad, Prabhakar2f5851b2014-05-16 10:33:21 -0300190 ycmux_mode = ret;
Lad, Prabhakar54a445a42014-05-16 10:33:09 -0300191 vpif_config_addr(ch, ret);
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300192 /* Get the next frame from the buffer queue */
193 common->next_frm = common->cur_frm =
194 list_entry(common->dma_queue.next,
195 struct vpif_disp_buffer, list);
196
197 list_del(&common->cur_frm->list);
Hans Verkuilc4697d72012-11-16 12:03:07 -0300198 spin_unlock_irqrestore(&common->irqlock, flags);
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300199 /* Mark state of the current frame to active */
200 common->cur_frm->vb.state = VB2_BUF_STATE_ACTIVE;
201
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300202 addr = vb2_dma_contig_plane_dma_addr(&common->cur_frm->vb, 0);
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300203 common->set_addr((addr + common->ytop_off),
204 (addr + common->ybtm_off),
205 (addr + common->ctop_off),
206 (addr + common->cbtm_off));
207
Lad, Prabhakar58334b02014-05-16 10:33:13 -0300208 /*
209 * Set interrupt for both the fields in VPIF
210 * Register enable channel in VPIF register
211 */
Lad, Prabhakar9e184042012-09-14 10:22:24 -0300212 channel_first_int[VPIF_VIDEO_INDEX][ch->channel_id] = 1;
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300213 if (VPIF_CHANNEL2_VIDEO == ch->channel_id) {
214 channel2_intr_assert();
215 channel2_intr_enable(1);
216 enable_channel2(1);
Lad, Prabhakar2bd4e582012-09-25 08:11:49 -0300217 if (vpif_config_data->chan_config[VPIF_CHANNEL2_VIDEO].clip_en)
Manjunath Hadli6964b102012-06-29 03:20:12 -0300218 channel2_clipping_enable(1);
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300219 }
220
Lad, Prabhakar2f5851b2014-05-16 10:33:21 -0300221 if (VPIF_CHANNEL3_VIDEO == ch->channel_id || ycmux_mode == 2) {
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300222 channel3_intr_assert();
223 channel3_intr_enable(1);
224 enable_channel3(1);
Lad, Prabhakar2bd4e582012-09-25 08:11:49 -0300225 if (vpif_config_data->chan_config[VPIF_CHANNEL3_VIDEO].clip_en)
Manjunath Hadli6964b102012-06-29 03:20:12 -0300226 channel3_clipping_enable(1);
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300227 }
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300228
229 return 0;
Lad, Prabhakar54a445a42014-05-16 10:33:09 -0300230
231err:
232 list_for_each_entry_safe(buf, tmp, &common->dma_queue, list) {
233 list_del(&buf->list);
234 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_QUEUED);
235 }
236
237 return ret;
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300238}
239
Lad, Prabhakar58334b02014-05-16 10:33:13 -0300240/**
241 * vpif_stop_streaming : Stop the DMA engine
242 * @vq: ptr to vb2_queue
243 *
244 * This callback stops the DMA engine and any remaining buffers
245 * in the DMA queue are released.
246 */
Hans Verkuile37559b2014-04-17 02:47:21 -0300247static void vpif_stop_streaming(struct vb2_queue *vq)
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300248{
Lad, Prabhakara2b235c2014-05-16 10:33:06 -0300249 struct channel_obj *ch = vb2_get_drv_priv(vq);
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300250 struct common_obj *common;
Hans Verkuilc4697d72012-11-16 12:03:07 -0300251 unsigned long flags;
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300252
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300253 common = &ch->common[VPIF_VIDEO_INDEX];
254
Lad, Prabhakar18c7adc2014-03-22 08:03:08 -0300255 /* Disable channel */
256 if (VPIF_CHANNEL2_VIDEO == ch->channel_id) {
257 enable_channel2(0);
258 channel2_intr_enable(0);
259 }
Lad, Prabhakar2f5851b2014-05-16 10:33:21 -0300260 if (VPIF_CHANNEL3_VIDEO == ch->channel_id || ycmux_mode == 2) {
Lad, Prabhakar18c7adc2014-03-22 08:03:08 -0300261 enable_channel3(0);
262 channel3_intr_enable(0);
263 }
Lad, Prabhakar18c7adc2014-03-22 08:03:08 -0300264
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300265 /* release all active buffers */
Hans Verkuilc4697d72012-11-16 12:03:07 -0300266 spin_lock_irqsave(&common->irqlock, flags);
Lad, Prabhakar18c7adc2014-03-22 08:03:08 -0300267 if (common->cur_frm == common->next_frm) {
268 vb2_buffer_done(&common->cur_frm->vb, VB2_BUF_STATE_ERROR);
269 } else {
270 if (common->cur_frm != NULL)
271 vb2_buffer_done(&common->cur_frm->vb,
272 VB2_BUF_STATE_ERROR);
273 if (common->next_frm != NULL)
274 vb2_buffer_done(&common->next_frm->vb,
275 VB2_BUF_STATE_ERROR);
276 }
277
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300278 while (!list_empty(&common->dma_queue)) {
279 common->next_frm = list_entry(common->dma_queue.next,
280 struct vpif_disp_buffer, list);
281 list_del(&common->next_frm->list);
282 vb2_buffer_done(&common->next_frm->vb, VB2_BUF_STATE_ERROR);
283 }
Hans Verkuilc4697d72012-11-16 12:03:07 -0300284 spin_unlock_irqrestore(&common->irqlock, flags);
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300285}
286
287static struct vb2_ops video_qops = {
288 .queue_setup = vpif_buffer_queue_setup,
Lad, Prabhakard10ed5c2014-05-16 10:33:08 -0300289 .wait_prepare = vb2_ops_wait_prepare,
290 .wait_finish = vb2_ops_wait_finish,
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300291 .buf_prepare = vpif_buffer_prepare,
292 .start_streaming = vpif_start_streaming,
293 .stop_streaming = vpif_stop_streaming,
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300294 .buf_queue = vpif_buffer_queue,
295};
296
Chaithrika U Se7332e32009-06-09 05:55:37 -0300297static void process_progressive_mode(struct common_obj *common)
298{
299 unsigned long addr = 0;
300
Hans Verkuilc4697d72012-11-16 12:03:07 -0300301 spin_lock(&common->irqlock);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300302 /* Get the next buffer from buffer queue */
303 common->next_frm = list_entry(common->dma_queue.next,
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300304 struct vpif_disp_buffer, list);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300305 /* Remove that buffer from the buffer queue */
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300306 list_del(&common->next_frm->list);
Hans Verkuilc4697d72012-11-16 12:03:07 -0300307 spin_unlock(&common->irqlock);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300308 /* Mark status of the buffer as active */
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300309 common->next_frm->vb.state = VB2_BUF_STATE_ACTIVE;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300310
311 /* Set top and bottom field addrs in VPIF registers */
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300312 addr = vb2_dma_contig_plane_dma_addr(&common->next_frm->vb, 0);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300313 common->set_addr(addr + common->ytop_off,
314 addr + common->ybtm_off,
315 addr + common->ctop_off,
316 addr + common->cbtm_off);
317}
318
319static void process_interlaced_mode(int fid, struct common_obj *common)
320{
321 /* device field id and local field id are in sync */
322 /* If this is even field */
323 if (0 == fid) {
324 if (common->cur_frm == common->next_frm)
325 return;
326
327 /* one frame is displayed If next frame is
328 * available, release cur_frm and move on */
329 /* Copy frame display time */
Sakari Ailus8e6057b2012-09-15 15:14:42 -0300330 v4l2_get_timestamp(&common->cur_frm->vb.v4l2_buf.timestamp);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300331 /* Change status of the cur_frm */
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300332 vb2_buffer_done(&common->cur_frm->vb,
333 VB2_BUF_STATE_DONE);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300334 /* Make cur_frm pointing to next_frm */
335 common->cur_frm = common->next_frm;
336
337 } else if (1 == fid) { /* odd field */
Hans Verkuilc4697d72012-11-16 12:03:07 -0300338 spin_lock(&common->irqlock);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300339 if (list_empty(&common->dma_queue)
340 || (common->cur_frm != common->next_frm)) {
Hans Verkuilc4697d72012-11-16 12:03:07 -0300341 spin_unlock(&common->irqlock);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300342 return;
343 }
Hans Verkuilc4697d72012-11-16 12:03:07 -0300344 spin_unlock(&common->irqlock);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300345 /* one field is displayed configure the next
346 * frame if it is available else hold on current
347 * frame */
348 /* Get next from the buffer queue */
349 process_progressive_mode(common);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300350 }
351}
352
353/*
354 * vpif_channel_isr: It changes status of the displayed buffer, takes next
355 * buffer from the queue and sets its address in VPIF registers
356 */
357static irqreturn_t vpif_channel_isr(int irq, void *dev_id)
358{
359 struct vpif_device *dev = &vpif_obj;
360 struct channel_obj *ch;
361 struct common_obj *common;
362 enum v4l2_field field;
363 int fid = -1, i;
364 int channel_id = 0;
365
366 channel_id = *(int *)(dev_id);
Manjunath Hadlib1fc4232012-04-13 04:43:10 -0300367 if (!vpif_intr_status(channel_id + 2))
368 return IRQ_NONE;
369
Chaithrika U Se7332e32009-06-09 05:55:37 -0300370 ch = dev->dev[channel_id];
371 field = ch->common[VPIF_VIDEO_INDEX].fmt.fmt.pix.field;
372 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 */
Sakari Ailus8e6057b2012-09-15 15:14:42 -0300388 v4l2_get_timestamp(&common->cur_frm->vb.
389 v4l2_buf.timestamp);
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300390 vb2_buffer_done(&common->cur_frm->vb,
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;
504 unsigned int hpitch, vpitch, sizeimage;
505
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;
518 vpitch = sizeimage / (hpitch * 2);
519 if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
520 (V4L2_FIELD_INTERLACED == vid_ch->buf_field)) {
521 common->ytop_off = 0;
522 common->ybtm_off = hpitch;
523 common->ctop_off = sizeimage / 2;
524 common->cbtm_off = sizeimage / 2 + hpitch;
525 } else if (V4L2_FIELD_SEQ_TB == vid_ch->buf_field) {
526 common->ytop_off = 0;
527 common->ybtm_off = sizeimage / 4;
528 common->ctop_off = sizeimage / 2;
529 common->cbtm_off = common->ctop_off + sizeimage / 4;
530 } else if (V4L2_FIELD_SEQ_BT == vid_ch->buf_field) {
531 common->ybtm_off = 0;
532 common->ytop_off = sizeimage / 4;
533 common->cbtm_off = sizeimage / 2;
534 common->ctop_off = common->cbtm_off + sizeimage / 4;
535 }
536
537 if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
538 (V4L2_FIELD_INTERLACED == vid_ch->buf_field)) {
539 vpifparams->video_params.storage_mode = 1;
540 } else {
541 vpifparams->video_params.storage_mode = 0;
542 }
543
544 if (ch->vpifparams.std_info.frm_fmt == 1) {
545 vpifparams->video_params.hpitch =
546 common->fmt.fmt.pix.bytesperline;
547 } else {
548 if ((field == V4L2_FIELD_ANY) ||
549 (field == V4L2_FIELD_INTERLACED))
550 vpifparams->video_params.hpitch =
551 common->fmt.fmt.pix.bytesperline * 2;
552 else
553 vpifparams->video_params.hpitch =
554 common->fmt.fmt.pix.bytesperline;
555 }
556
557 ch->vpifparams.video_params.stdid = ch->vpifparams.std_info.stdid;
558}
559
Chaithrika U Se7332e32009-06-09 05:55:37 -0300560static void vpif_config_addr(struct channel_obj *ch, int muxmode)
561{
562 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
563
564 if (VPIF_CHANNEL3_VIDEO == ch->channel_id) {
565 common->set_addr = ch3_set_videobuf_addr;
566 } else {
567 if (2 == muxmode)
568 common->set_addr = ch2_set_videobuf_addr_yc_nmux;
569 else
570 common->set_addr = ch2_set_videobuf_addr;
571 }
572}
573
Chaithrika U Se7332e32009-06-09 05:55:37 -0300574/* functions implementing ioctls */
Mats Randgaard2c0ddd12010-12-16 12:17:45 -0300575/**
576 * vpif_querycap() - QUERYCAP handler
577 * @file: file ptr
578 * @priv: file handle
579 * @cap: ptr to v4l2_capability structure
580 */
Chaithrika U Se7332e32009-06-09 05:55:37 -0300581static int vpif_querycap(struct file *file, void *priv,
582 struct v4l2_capability *cap)
583{
Muralidharan Karicheri317b2e22009-09-16 14:30:53 -0300584 struct vpif_display_config *config = vpif_dev->platform_data;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300585
Lad, Prabhakar626d533f2012-09-25 11:21:55 -0300586 cap->device_caps = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING;
587 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
Lad, Prabhakar76c4c2b2014-05-16 10:33:22 -0300588 strlcpy(cap->driver, VPIF_DRIVER_NAME, sizeof(cap->driver));
Lad, Prabhakar626d533f2012-09-25 11:21:55 -0300589 snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
590 dev_name(vpif_dev));
Chaithrika U Se7332e32009-06-09 05:55:37 -0300591 strlcpy(cap->card, config->card_name, sizeof(cap->card));
592
593 return 0;
594}
595
596static int vpif_enum_fmt_vid_out(struct file *file, void *priv,
597 struct v4l2_fmtdesc *fmt)
598{
Lad, Prabhakar9cba6532014-05-16 10:33:30 -0300599 if (fmt->index != 0)
Chaithrika U Se7332e32009-06-09 05:55:37 -0300600 return -EINVAL;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300601
602 /* Fill in the information about format */
603 fmt->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
604 strcpy(fmt->description, "YCbCr4:2:2 YC Planar");
605 fmt->pixelformat = V4L2_PIX_FMT_YUV422P;
Lad, Prabhakar9cba6532014-05-16 10:33:30 -0300606 fmt->flags = 0;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300607 return 0;
608}
609
610static int vpif_g_fmt_vid_out(struct file *file, void *priv,
611 struct v4l2_format *fmt)
612{
Lad, Prabhakarfcc22af2014-05-16 10:33:15 -0300613 struct video_device *vdev = video_devdata(file);
614 struct channel_obj *ch = video_get_drvdata(vdev);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300615 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
616
617 /* Check the validity of the buffer type */
618 if (common->fmt.type != fmt->type)
619 return -EINVAL;
620
Mats Randgaardc027e162010-12-16 12:17:44 -0300621 if (vpif_update_resolution(ch))
Hans Verkuil9bfaae22011-01-05 13:35:45 -0300622 return -EINVAL;
623 *fmt = common->fmt;
624 return 0;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300625}
626
Chaithrika U Se7332e32009-06-09 05:55:37 -0300627static int vpif_try_fmt_vid_out(struct file *file, void *priv,
628 struct v4l2_format *fmt)
629{
Lad, Prabhakarfcc22af2014-05-16 10:33:15 -0300630 struct video_device *vdev = video_devdata(file);
631 struct channel_obj *ch = video_get_drvdata(vdev);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300632 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
633 struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300634
Lad, Prabhakar9cba6532014-05-16 10:33:30 -0300635 /*
636 * to supress v4l-compliance warnings silently correct
637 * the pixelformat
638 */
639 if (pixfmt->pixelformat != V4L2_PIX_FMT_YUV422P)
640 pixfmt->pixelformat = common->fmt.fmt.pix.pixelformat;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300641
Lad, Prabhakar9cba6532014-05-16 10:33:30 -0300642 if (vpif_update_resolution(ch))
643 return -EINVAL;
644
645 pixfmt->colorspace = common->fmt.fmt.pix.colorspace;
646 pixfmt->field = common->fmt.fmt.pix.field;
647 pixfmt->bytesperline = common->fmt.fmt.pix.width;
648 pixfmt->width = common->fmt.fmt.pix.width;
649 pixfmt->height = common->fmt.fmt.pix.height;
650 pixfmt->sizeimage = pixfmt->bytesperline * pixfmt->height * 2;
Lad, Prabhakar9cba6532014-05-16 10:33:30 -0300651
652 return 0;
653}
654
655static int vpif_s_fmt_vid_out(struct file *file, void *priv,
656 struct v4l2_format *fmt)
657{
658 struct video_device *vdev = video_devdata(file);
659 struct channel_obj *ch = video_get_drvdata(vdev);
660 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
661 struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
662 int ret;
663
664 if (vb2_is_busy(&common->buffer_queue))
665 return -EBUSY;
666
667 ret = vpif_try_fmt_vid_out(file, priv, fmt);
668 if (ret)
669 return ret;
670
671 /* store the pix format in the channel object */
672 common->fmt.fmt.pix = *pixfmt;
673
674 /* store the format in the channel object */
675 common->fmt = *fmt;
676 return 0;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300677}
678
Hans Verkuil314527a2013-03-15 06:10:40 -0300679static int vpif_s_std(struct file *file, void *priv, v4l2_std_id std_id)
Chaithrika U Se7332e32009-06-09 05:55:37 -0300680{
Lad, Prabhakarf27d0f42014-05-16 10:33:28 -0300681 struct vpif_display_config *config = vpif_dev->platform_data;
Lad, Prabhakarfcc22af2014-05-16 10:33:15 -0300682 struct video_device *vdev = video_devdata(file);
683 struct channel_obj *ch = video_get_drvdata(vdev);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300684 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
Lad, Prabhakarf27d0f42014-05-16 10:33:28 -0300685 struct vpif_display_chan_config *chan_cfg;
686 struct v4l2_output output;
687 int ret;
688
689 if (config->chan_config[ch->channel_id].outputs == NULL)
690 return -ENODATA;
691
692 chan_cfg = &config->chan_config[ch->channel_id];
693 output = chan_cfg->outputs[ch->output_idx].output;
694 if (output.capabilities != V4L2_OUT_CAP_STD)
695 return -ENODATA;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300696
Lad, Prabhakar2f5851b2014-05-16 10:33:21 -0300697 if (vb2_is_busy(&common->buffer_queue))
698 return -EBUSY;
699
Lad, Prabhakarf27d0f42014-05-16 10:33:28 -0300700
Hans Verkuil314527a2013-03-15 06:10:40 -0300701 if (!(std_id & VPIF_V4L2_STD))
Chaithrika U Se7332e32009-06-09 05:55:37 -0300702 return -EINVAL;
703
Chaithrika U Se7332e32009-06-09 05:55:37 -0300704 /* Call encoder subdevice function to set the standard */
Hans Verkuil314527a2013-03-15 06:10:40 -0300705 ch->video.stdid = std_id;
Hans Verkuil0598c172012-09-18 07:18:47 -0300706 memset(&ch->video.dv_timings, 0, sizeof(ch->video.dv_timings));
Chaithrika U Se7332e32009-06-09 05:55:37 -0300707 /* Get the information about the standard */
Hans Verkuil9bfaae22011-01-05 13:35:45 -0300708 if (vpif_update_resolution(ch))
709 return -EINVAL;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300710
Chaithrika U Se7332e32009-06-09 05:55:37 -0300711 common->fmt.fmt.pix.bytesperline = common->fmt.fmt.pix.width;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300712
713 ret = v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 1, video,
Hans Verkuil314527a2013-03-15 06:10:40 -0300714 s_std_output, std_id);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300715 if (ret < 0) {
716 vpif_err("Failed to set output standard\n");
Hans Verkuil9bfaae22011-01-05 13:35:45 -0300717 return ret;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300718 }
719
Laurent Pinchart8774bed2014-04-28 16:53:01 -0300720 ret = v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 1, video,
Hans Verkuil314527a2013-03-15 06:10:40 -0300721 s_std, std_id);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300722 if (ret < 0)
723 vpif_err("Failed to set standard for sub devices\n");
Chaithrika U Se7332e32009-06-09 05:55:37 -0300724 return ret;
725}
726
727static int vpif_g_std(struct file *file, void *priv, v4l2_std_id *std)
728{
Lad, Prabhakarf27d0f42014-05-16 10:33:28 -0300729 struct vpif_display_config *config = vpif_dev->platform_data;
Lad, Prabhakarfcc22af2014-05-16 10:33:15 -0300730 struct video_device *vdev = video_devdata(file);
731 struct channel_obj *ch = video_get_drvdata(vdev);
Lad, Prabhakarf27d0f42014-05-16 10:33:28 -0300732 struct vpif_display_chan_config *chan_cfg;
733 struct v4l2_output output;
734
735 if (config->chan_config[ch->channel_id].outputs == NULL)
736 return -ENODATA;
737
738 chan_cfg = &config->chan_config[ch->channel_id];
739 output = chan_cfg->outputs[ch->output_idx].output;
740 if (output.capabilities != V4L2_OUT_CAP_STD)
741 return -ENODATA;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300742
743 *std = ch->video.stdid;
744 return 0;
745}
746
Chaithrika U Se7332e32009-06-09 05:55:37 -0300747static int vpif_enum_output(struct file *file, void *fh,
748 struct v4l2_output *output)
749{
750
Muralidharan Karicheri317b2e22009-09-16 14:30:53 -0300751 struct vpif_display_config *config = vpif_dev->platform_data;
Lad, Prabhakarfcc22af2014-05-16 10:33:15 -0300752 struct video_device *vdev = video_devdata(file);
753 struct channel_obj *ch = video_get_drvdata(vdev);
Lad, Prabhakar2bd4e582012-09-25 08:11:49 -0300754 struct vpif_display_chan_config *chan_cfg;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300755
Lad, Prabhakar2bd4e582012-09-25 08:11:49 -0300756 chan_cfg = &config->chan_config[ch->channel_id];
757 if (output->index >= chan_cfg->output_count) {
Chaithrika U Se7332e32009-06-09 05:55:37 -0300758 vpif_dbg(1, debug, "Invalid output index\n");
759 return -EINVAL;
760 }
761
Lad, Prabhakar2bd4e582012-09-25 08:11:49 -0300762 *output = chan_cfg->outputs[output->index].output;
763 return 0;
764}
Chaithrika U Se7332e32009-06-09 05:55:37 -0300765
Lad, Prabhakar2bd4e582012-09-25 08:11:49 -0300766/**
767 * vpif_output_to_subdev() - Maps output to sub device
768 * @vpif_cfg - global config ptr
769 * @chan_cfg - channel config ptr
770 * @index - Given output index from application
771 *
772 * lookup the sub device information for a given output index.
773 * we report all the output to application. output table also
774 * has sub device name for the each output
775 */
776static int
777vpif_output_to_subdev(struct vpif_display_config *vpif_cfg,
778 struct vpif_display_chan_config *chan_cfg, int index)
779{
780 struct vpif_subdev_info *subdev_info;
781 const char *subdev_name;
782 int i;
783
784 vpif_dbg(2, debug, "vpif_output_to_subdev\n");
785
786 if (chan_cfg->outputs == NULL)
787 return -1;
788
789 subdev_name = chan_cfg->outputs[index].subdev_name;
790 if (subdev_name == NULL)
791 return -1;
792
793 /* loop through the sub device list to get the sub device info */
794 for (i = 0; i < vpif_cfg->subdev_count; i++) {
795 subdev_info = &vpif_cfg->subdevinfo[i];
796 if (!strcmp(subdev_info->name, subdev_name))
797 return i;
798 }
799 return -1;
800}
801
802/**
803 * vpif_set_output() - Select an output
804 * @vpif_cfg - global config ptr
805 * @ch - channel
806 * @index - Given output index from application
807 *
808 * Select the given output.
809 */
810static int vpif_set_output(struct vpif_display_config *vpif_cfg,
811 struct channel_obj *ch, int index)
812{
813 struct vpif_display_chan_config *chan_cfg =
814 &vpif_cfg->chan_config[ch->channel_id];
815 struct vpif_subdev_info *subdev_info = NULL;
816 struct v4l2_subdev *sd = NULL;
817 u32 input = 0, output = 0;
818 int sd_index;
819 int ret;
820
821 sd_index = vpif_output_to_subdev(vpif_cfg, chan_cfg, index);
822 if (sd_index >= 0) {
823 sd = vpif_obj.sd[sd_index];
824 subdev_info = &vpif_cfg->subdevinfo[sd_index];
825 }
826
827 if (sd) {
828 input = chan_cfg->outputs[index].input_route;
829 output = chan_cfg->outputs[index].output_route;
830 ret = v4l2_subdev_call(sd, video, s_routing, input, output, 0);
831 if (ret < 0 && ret != -ENOIOCTLCMD) {
832 vpif_err("Failed to set output\n");
833 return ret;
834 }
835
836 }
837 ch->output_idx = index;
838 ch->sd = sd;
839 if (chan_cfg->outputs != NULL)
840 /* update tvnorms from the sub device output info */
841 ch->video_dev->tvnorms = chan_cfg->outputs[index].output.std;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300842 return 0;
843}
844
845static int vpif_s_output(struct file *file, void *priv, unsigned int i)
846{
Lad, Prabhakar2bd4e582012-09-25 08:11:49 -0300847 struct vpif_display_config *config = vpif_dev->platform_data;
Lad, Prabhakarfcc22af2014-05-16 10:33:15 -0300848 struct video_device *vdev = video_devdata(file);
849 struct channel_obj *ch = video_get_drvdata(vdev);
Lad, Prabhakar2bd4e582012-09-25 08:11:49 -0300850 struct vpif_display_chan_config *chan_cfg;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300851 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
Lad, Prabhakar2bd4e582012-09-25 08:11:49 -0300852
Lad, Prabhakar2f5851b2014-05-16 10:33:21 -0300853 if (vb2_is_busy(&common->buffer_queue))
854 return -EBUSY;
855
Lad, Prabhakar2bd4e582012-09-25 08:11:49 -0300856 chan_cfg = &config->chan_config[ch->channel_id];
857
858 if (i >= chan_cfg->output_count)
859 return -EINVAL;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300860
Lad, Prabhakar2bd4e582012-09-25 08:11:49 -0300861 return vpif_set_output(config, ch, i);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300862}
863
864static int vpif_g_output(struct file *file, void *priv, unsigned int *i)
865{
Lad, Prabhakarfcc22af2014-05-16 10:33:15 -0300866 struct video_device *vdev = video_devdata(file);
867 struct channel_obj *ch = video_get_drvdata(vdev);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300868
Hans Verkuil311673e2012-09-20 09:06:23 -0300869 *i = ch->output_idx;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300870
871 return 0;
872}
873
Mats Randgaard40c8bce2010-12-16 12:17:43 -0300874/**
Hans Verkuil0598c172012-09-18 07:18:47 -0300875 * vpif_enum_dv_timings() - ENUM_DV_TIMINGS handler
Mats Randgaard40c8bce2010-12-16 12:17:43 -0300876 * @file: file ptr
877 * @priv: file handle
Hans Verkuil0598c172012-09-18 07:18:47 -0300878 * @timings: input timings
Mats Randgaard40c8bce2010-12-16 12:17:43 -0300879 */
Hans Verkuil0598c172012-09-18 07:18:47 -0300880static int
881vpif_enum_dv_timings(struct file *file, void *priv,
882 struct v4l2_enum_dv_timings *timings)
Mats Randgaard40c8bce2010-12-16 12:17:43 -0300883{
Lad, Prabhakar1093c592014-05-16 10:33:27 -0300884 struct vpif_display_config *config = vpif_dev->platform_data;
Lad, Prabhakarfcc22af2014-05-16 10:33:15 -0300885 struct video_device *vdev = video_devdata(file);
886 struct channel_obj *ch = video_get_drvdata(vdev);
Lad, Prabhakar1093c592014-05-16 10:33:27 -0300887 struct vpif_display_chan_config *chan_cfg;
888 struct v4l2_output output;
Lad, Prabhakar2bd4e582012-09-25 08:11:49 -0300889 int ret;
Mats Randgaard40c8bce2010-12-16 12:17:43 -0300890
Lad, Prabhakar1093c592014-05-16 10:33:27 -0300891 if (config->chan_config[ch->channel_id].outputs == NULL)
892 return -ENODATA;
893
894 chan_cfg = &config->chan_config[ch->channel_id];
895 output = chan_cfg->outputs[ch->output_idx].output;
896 if (output.capabilities != V4L2_OUT_CAP_DV_TIMINGS)
897 return -ENODATA;
898
Laurent Pinchart10d21462014-01-31 09:04:19 -0300899 timings->pad = 0;
900
901 ret = v4l2_subdev_call(ch->sd, pad, enum_dv_timings, timings);
Wei Yongjun63af4af2012-10-30 09:49:38 -0300902 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
Lad, Prabhakar2bd4e582012-09-25 08:11:49 -0300903 return -EINVAL;
904 return ret;
Mats Randgaard40c8bce2010-12-16 12:17:43 -0300905}
906
907/**
Mats Randgaardc027e162010-12-16 12:17:44 -0300908 * vpif_s_dv_timings() - S_DV_TIMINGS handler
909 * @file: file ptr
910 * @priv: file handle
911 * @timings: digital video timings
912 */
913static int vpif_s_dv_timings(struct file *file, void *priv,
914 struct v4l2_dv_timings *timings)
915{
Lad, Prabhakar1093c592014-05-16 10:33:27 -0300916 struct vpif_display_config *config = vpif_dev->platform_data;
Lad, Prabhakarfcc22af2014-05-16 10:33:15 -0300917 struct video_device *vdev = video_devdata(file);
918 struct channel_obj *ch = video_get_drvdata(vdev);
Mats Randgaardc027e162010-12-16 12:17:44 -0300919 struct vpif_params *vpifparams = &ch->vpifparams;
Lad, Prabhakar1093c592014-05-16 10:33:27 -0300920 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
Mats Randgaardc027e162010-12-16 12:17:44 -0300921 struct vpif_channel_config_params *std_info = &vpifparams->std_info;
922 struct video_obj *vid_ch = &ch->video;
Hans Verkuil0598c172012-09-18 07:18:47 -0300923 struct v4l2_bt_timings *bt = &vid_ch->dv_timings.bt;
Lad, Prabhakar1093c592014-05-16 10:33:27 -0300924 struct vpif_display_chan_config *chan_cfg;
925 struct v4l2_output output;
Mats Randgaardc027e162010-12-16 12:17:44 -0300926 int ret;
927
Lad, Prabhakar1093c592014-05-16 10:33:27 -0300928 if (config->chan_config[ch->channel_id].outputs == NULL)
929 return -ENODATA;
930
931 chan_cfg = &config->chan_config[ch->channel_id];
932 output = chan_cfg->outputs[ch->output_idx].output;
933 if (output.capabilities != V4L2_OUT_CAP_DV_TIMINGS)
934 return -ENODATA;
935
936 if (vb2_is_busy(&common->buffer_queue))
937 return -EBUSY;
938
Mats Randgaardc027e162010-12-16 12:17:44 -0300939 if (timings->type != V4L2_DV_BT_656_1120) {
940 vpif_dbg(2, debug, "Timing type not defined\n");
941 return -EINVAL;
942 }
943
944 /* Configure subdevice timings, if any */
Hans Verkuil882084a2012-09-20 09:06:31 -0300945 ret = v4l2_subdev_call(ch->sd, video, s_dv_timings, timings);
Lad, Prabhakar2bd4e582012-09-25 08:11:49 -0300946 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
947 ret = 0;
948 if (ret < 0) {
Mats Randgaardc027e162010-12-16 12:17:44 -0300949 vpif_dbg(2, debug, "Error setting custom DV timings\n");
950 return ret;
951 }
952
953 if (!(timings->bt.width && timings->bt.height &&
954 (timings->bt.hbackporch ||
955 timings->bt.hfrontporch ||
956 timings->bt.hsync) &&
957 timings->bt.vfrontporch &&
958 (timings->bt.vbackporch ||
959 timings->bt.vsync))) {
960 vpif_dbg(2, debug, "Timings for width, height, "
961 "horizontal back porch, horizontal sync, "
962 "horizontal front porch, vertical back porch, "
963 "vertical sync and vertical back porch "
964 "must be defined\n");
965 return -EINVAL;
966 }
967
Hans Verkuil0598c172012-09-18 07:18:47 -0300968 vid_ch->dv_timings = *timings;
Mats Randgaardc027e162010-12-16 12:17:44 -0300969
970 /* Configure video port timings */
971
Hans Verkuile3655262013-07-29 08:41:00 -0300972 std_info->eav2sav = V4L2_DV_BT_BLANKING_WIDTH(bt) - 8;
Mats Randgaardc027e162010-12-16 12:17:44 -0300973 std_info->sav2eav = bt->width;
974
975 std_info->l1 = 1;
976 std_info->l3 = bt->vsync + bt->vbackporch + 1;
977
Hans Verkuile3655262013-07-29 08:41:00 -0300978 std_info->vsize = V4L2_DV_BT_FRAME_HEIGHT(bt);
Mats Randgaardc027e162010-12-16 12:17:44 -0300979 if (bt->interlaced) {
980 if (bt->il_vbackporch || bt->il_vfrontporch || bt->il_vsync) {
Mats Randgaardc027e162010-12-16 12:17:44 -0300981 std_info->l5 = std_info->vsize/2 -
982 (bt->vfrontporch - 1);
983 std_info->l7 = std_info->vsize/2 + 1;
984 std_info->l9 = std_info->l7 + bt->il_vsync +
985 bt->il_vbackporch + 1;
986 std_info->l11 = std_info->vsize -
987 (bt->il_vfrontporch - 1);
988 } else {
989 vpif_dbg(2, debug, "Required timing values for "
990 "interlaced BT format missing\n");
991 return -EINVAL;
992 }
993 } else {
Mats Randgaardc027e162010-12-16 12:17:44 -0300994 std_info->l5 = std_info->vsize - (bt->vfrontporch - 1);
995 }
996 strncpy(std_info->name, "Custom timings BT656/1120",
997 VPIF_MAX_NAME);
998 std_info->width = bt->width;
999 std_info->height = bt->height;
1000 std_info->frm_fmt = bt->interlaced ? 0 : 1;
1001 std_info->ycmux_mode = 0;
1002 std_info->capture_format = 0;
1003 std_info->vbi_supported = 0;
1004 std_info->hd_sd = 1;
1005 std_info->stdid = 0;
Mats Randgaardc027e162010-12-16 12:17:44 -03001006 vid_ch->stdid = 0;
Mats Randgaardc027e162010-12-16 12:17:44 -03001007
1008 return 0;
1009}
1010
1011/**
1012 * vpif_g_dv_timings() - G_DV_TIMINGS handler
1013 * @file: file ptr
1014 * @priv: file handle
1015 * @timings: digital video timings
1016 */
1017static int vpif_g_dv_timings(struct file *file, void *priv,
1018 struct v4l2_dv_timings *timings)
1019{
Lad, Prabhakar1093c592014-05-16 10:33:27 -03001020 struct vpif_display_config *config = vpif_dev->platform_data;
Lad, Prabhakarfcc22af2014-05-16 10:33:15 -03001021 struct video_device *vdev = video_devdata(file);
1022 struct channel_obj *ch = video_get_drvdata(vdev);
Lad, Prabhakar1093c592014-05-16 10:33:27 -03001023 struct vpif_display_chan_config *chan_cfg;
Mats Randgaardc027e162010-12-16 12:17:44 -03001024 struct video_obj *vid_ch = &ch->video;
Lad, Prabhakar1093c592014-05-16 10:33:27 -03001025 struct v4l2_output output;
1026
1027 if (config->chan_config[ch->channel_id].outputs == NULL)
1028 goto error;
1029
1030 chan_cfg = &config->chan_config[ch->channel_id];
1031 output = chan_cfg->outputs[ch->output_idx].output;
1032
1033 if (output.capabilities != V4L2_OUT_CAP_DV_TIMINGS)
1034 goto error;
Mats Randgaardc027e162010-12-16 12:17:44 -03001035
Hans Verkuil0598c172012-09-18 07:18:47 -03001036 *timings = vid_ch->dv_timings;
Mats Randgaardc027e162010-12-16 12:17:44 -03001037
1038 return 0;
Lad, Prabhakar1093c592014-05-16 10:33:27 -03001039error:
1040 return -ENODATA;
Mats Randgaardc027e162010-12-16 12:17:44 -03001041}
Mats Randgaard7036d6a2010-12-16 12:17:41 -03001042
1043/*
Mats Randgaard7036d6a2010-12-16 12:17:41 -03001044 * vpif_log_status() - Status information
1045 * @file: file ptr
1046 * @priv: file handle
1047 *
1048 * Returns zero.
1049 */
1050static int vpif_log_status(struct file *filep, void *priv)
1051{
1052 /* status for sub devices */
1053 v4l2_device_call_all(&vpif_obj.v4l2_dev, 0, core, log_status);
1054
1055 return 0;
1056}
1057
Chaithrika U Se7332e32009-06-09 05:55:37 -03001058/* vpif display ioctl operations */
1059static const struct v4l2_ioctl_ops vpif_ioctl_ops = {
Lad, Prabhakardcce8662014-05-16 10:33:25 -03001060 .vidioc_querycap = vpif_querycap,
Chaithrika U Se7332e32009-06-09 05:55:37 -03001061 .vidioc_enum_fmt_vid_out = vpif_enum_fmt_vid_out,
Lad, Prabhakardcce8662014-05-16 10:33:25 -03001062 .vidioc_g_fmt_vid_out = vpif_g_fmt_vid_out,
1063 .vidioc_s_fmt_vid_out = vpif_s_fmt_vid_out,
1064 .vidioc_try_fmt_vid_out = vpif_try_fmt_vid_out,
Lad, Prabhakarb92cde12014-05-16 10:33:16 -03001065
1066 .vidioc_reqbufs = vb2_ioctl_reqbufs,
1067 .vidioc_create_bufs = vb2_ioctl_create_bufs,
1068 .vidioc_querybuf = vb2_ioctl_querybuf,
1069 .vidioc_qbuf = vb2_ioctl_qbuf,
1070 .vidioc_dqbuf = vb2_ioctl_dqbuf,
1071 .vidioc_expbuf = vb2_ioctl_expbuf,
1072 .vidioc_streamon = vb2_ioctl_streamon,
1073 .vidioc_streamoff = vb2_ioctl_streamoff,
1074
Lad, Prabhakardcce8662014-05-16 10:33:25 -03001075 .vidioc_s_std = vpif_s_std,
Chaithrika U Se7332e32009-06-09 05:55:37 -03001076 .vidioc_g_std = vpif_g_std,
Lad, Prabhakardcce8662014-05-16 10:33:25 -03001077
Chaithrika U Se7332e32009-06-09 05:55:37 -03001078 .vidioc_enum_output = vpif_enum_output,
1079 .vidioc_s_output = vpif_s_output,
1080 .vidioc_g_output = vpif_g_output,
Lad, Prabhakardcce8662014-05-16 10:33:25 -03001081
1082 .vidioc_enum_dv_timings = vpif_enum_dv_timings,
1083 .vidioc_s_dv_timings = vpif_s_dv_timings,
1084 .vidioc_g_dv_timings = vpif_g_dv_timings,
1085
Mats Randgaard7036d6a2010-12-16 12:17:41 -03001086 .vidioc_log_status = vpif_log_status,
Chaithrika U Se7332e32009-06-09 05:55:37 -03001087};
1088
1089static const struct v4l2_file_operations vpif_fops = {
1090 .owner = THIS_MODULE,
Lad, Prabhakarfcc22af2014-05-16 10:33:15 -03001091 .open = v4l2_fh_open,
1092 .release = vb2_fop_release,
Hans Verkuil9bfaae22011-01-05 13:35:45 -03001093 .unlocked_ioctl = video_ioctl2,
Lad, Prabhakar4be21532014-05-16 10:33:14 -03001094 .mmap = vb2_fop_mmap,
1095 .poll = vb2_fop_poll
Chaithrika U Se7332e32009-06-09 05:55:37 -03001096};
1097
Chaithrika U Se7332e32009-06-09 05:55:37 -03001098/*Configure the channels, buffer sizei, request irq */
1099static int initialize_vpif(void)
1100{
1101 int free_channel_objects_index;
Lad, Prabhakard5b94b92014-05-16 10:33:23 -03001102 int err, i, j;
Chaithrika U Se7332e32009-06-09 05:55:37 -03001103
1104 /* Allocate memory for six channel objects */
1105 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1106 vpif_obj.dev[i] =
Mats Randgaard1f8766b2010-08-30 10:30:37 -03001107 kzalloc(sizeof(struct channel_obj), GFP_KERNEL);
Chaithrika U Se7332e32009-06-09 05:55:37 -03001108 /* If memory allocation fails, return error */
1109 if (!vpif_obj.dev[i]) {
1110 free_channel_objects_index = i;
1111 err = -ENOMEM;
1112 goto vpif_init_free_channel_objects;
1113 }
1114 }
1115
Chaithrika U Se7332e32009-06-09 05:55:37 -03001116 return 0;
1117
1118vpif_init_free_channel_objects:
1119 for (j = 0; j < free_channel_objects_index; j++)
1120 kfree(vpif_obj.dev[j]);
1121 return err;
1122}
1123
Lad, Prabhakar4b8a5312013-06-25 11:17:35 -03001124static int vpif_async_bound(struct v4l2_async_notifier *notifier,
1125 struct v4l2_subdev *subdev,
1126 struct v4l2_async_subdev *asd)
1127{
1128 int i;
1129
1130 for (i = 0; i < vpif_obj.config->subdev_count; i++)
1131 if (!strcmp(vpif_obj.config->subdevinfo[i].name,
1132 subdev->name)) {
1133 vpif_obj.sd[i] = subdev;
1134 vpif_obj.sd[i]->grp_id = 1 << i;
1135 return 0;
1136 }
1137
1138 return -EINVAL;
1139}
1140
1141static int vpif_probe_complete(void)
1142{
1143 struct common_obj *common;
Lad, Prabhakar4be21532014-05-16 10:33:14 -03001144 struct video_device *vdev;
Lad, Prabhakar4b8a5312013-06-25 11:17:35 -03001145 struct channel_obj *ch;
Lad, Prabhakara2b235c2014-05-16 10:33:06 -03001146 struct vb2_queue *q;
Lad, Prabhakar4b8a5312013-06-25 11:17:35 -03001147 int j, err, k;
1148
1149 for (j = 0; j < VPIF_DISPLAY_MAX_DEVICES; j++) {
1150 ch = vpif_obj.dev[j];
1151 /* Initialize field of the channel objects */
Lad, Prabhakar4b8a5312013-06-25 11:17:35 -03001152 for (k = 0; k < VPIF_NUMOBJECTS; k++) {
Lad, Prabhakar4b8a5312013-06-25 11:17:35 -03001153 common = &ch->common[k];
Lad, Prabhakar4b8a5312013-06-25 11:17:35 -03001154 spin_lock_init(&common->irqlock);
1155 mutex_init(&common->lock);
Lad, Prabhakar4b8a5312013-06-25 11:17:35 -03001156 common->set_addr = NULL;
1157 common->ytop_off = 0;
1158 common->ybtm_off = 0;
1159 common->ctop_off = 0;
1160 common->cbtm_off = 0;
1161 common->cur_frm = NULL;
1162 common->next_frm = NULL;
1163 memset(&common->fmt, 0, sizeof(common->fmt));
Lad, Prabhakar4b8a5312013-06-25 11:17:35 -03001164 }
1165 ch->initialized = 0;
1166 if (vpif_obj.config->subdev_count)
1167 ch->sd = vpif_obj.sd[0];
1168 ch->channel_id = j;
Lad, Prabhakar4b8a5312013-06-25 11:17:35 -03001169
1170 memset(&ch->vpifparams, 0, sizeof(ch->vpifparams));
1171
Lad, Prabhakar4b8a5312013-06-25 11:17:35 -03001172 ch->common[VPIF_VIDEO_INDEX].fmt.type =
1173 V4L2_BUF_TYPE_VIDEO_OUTPUT;
Lad, Prabhakar4b8a5312013-06-25 11:17:35 -03001174
1175 /* select output 0 */
1176 err = vpif_set_output(vpif_obj.config, ch, 0);
1177 if (err)
1178 goto probe_out;
1179
Lad, Prabhakar9cba6532014-05-16 10:33:30 -03001180 /* set initial format */
1181 ch->video.stdid = V4L2_STD_525_60;
1182 memset(&ch->video.dv_timings, 0, sizeof(ch->video.dv_timings));
1183 vpif_update_resolution(ch);
1184
Lad, Prabhakara2b235c2014-05-16 10:33:06 -03001185 /* Initialize vb2 queue */
1186 q = &common->buffer_queue;
1187 q->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
1188 q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
1189 q->drv_priv = ch;
1190 q->ops = &video_qops;
1191 q->mem_ops = &vb2_dma_contig_memops;
1192 q->buf_struct_size = sizeof(struct vpif_disp_buffer);
1193 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1194 q->min_buffers_needed = 1;
Lad, Prabhakard10ed5c2014-05-16 10:33:08 -03001195 q->lock = &common->lock;
Lad, Prabhakara2b235c2014-05-16 10:33:06 -03001196 err = vb2_queue_init(q);
1197 if (err) {
1198 vpif_err("vpif_display: vb2_queue_init() failed\n");
1199 goto probe_out;
1200 }
1201
1202 common->alloc_ctx = vb2_dma_contig_init_ctx(vpif_dev);
1203 if (IS_ERR(common->alloc_ctx)) {
1204 vpif_err("Failed to get the context\n");
1205 err = PTR_ERR(common->alloc_ctx);
1206 goto probe_out;
1207 }
1208
1209 INIT_LIST_HEAD(&common->dma_queue);
1210
Lad, Prabhakar4b8a5312013-06-25 11:17:35 -03001211 /* register video device */
1212 vpif_dbg(1, debug, "channel=%x,channel->video_dev=%x\n",
1213 (int)ch, (int)&ch->video_dev);
1214
Lad, Prabhakar76c4c2b2014-05-16 10:33:22 -03001215 /* Initialize the video_device structure */
Lad, Prabhakar4be21532014-05-16 10:33:14 -03001216 vdev = ch->video_dev;
Lad, Prabhakar76c4c2b2014-05-16 10:33:22 -03001217 strlcpy(vdev->name, VPIF_DRIVER_NAME, sizeof(vdev->name));
1218 vdev->release = video_device_release;
1219 vdev->fops = &vpif_fops;
1220 vdev->ioctl_ops = &vpif_ioctl_ops;
1221 vdev->v4l2_dev = &vpif_obj.v4l2_dev;
1222 vdev->vfl_dir = VFL_DIR_TX;
Lad, Prabhakar4be21532014-05-16 10:33:14 -03001223 vdev->queue = q;
Lad, Prabhakarfcc22af2014-05-16 10:33:15 -03001224 vdev->lock = &common->lock;
Lad, Prabhakarfcc22af2014-05-16 10:33:15 -03001225 video_set_drvdata(ch->video_dev, ch);
Lad, Prabhakar4be21532014-05-16 10:33:14 -03001226 err = video_register_device(vdev, VFL_TYPE_GRABBER,
1227 (j ? 3 : 2));
Lad, Prabhakar4b8a5312013-06-25 11:17:35 -03001228 if (err < 0)
1229 goto probe_out;
1230 }
1231
1232 return 0;
1233
1234probe_out:
1235 for (k = 0; k < j; k++) {
1236 ch = vpif_obj.dev[k];
Lad, Prabhakara2b235c2014-05-16 10:33:06 -03001237 common = &ch->common[k];
1238 vb2_dma_contig_cleanup_ctx(common->alloc_ctx);
Lad, Prabhakar4b8a5312013-06-25 11:17:35 -03001239 video_unregister_device(ch->video_dev);
1240 video_device_release(ch->video_dev);
1241 ch->video_dev = NULL;
1242 }
1243 return err;
1244}
1245
1246static int vpif_async_complete(struct v4l2_async_notifier *notifier)
1247{
1248 return vpif_probe_complete();
1249}
1250
Chaithrika U Se7332e32009-06-09 05:55:37 -03001251/*
1252 * vpif_probe: This function creates device entries by register itself to the
1253 * V4L2 driver and initializes fields of each channel objects
1254 */
1255static __init int vpif_probe(struct platform_device *pdev)
1256{
Muralidharan Karicheri317b2e22009-09-16 14:30:53 -03001257 struct vpif_subdev_info *subdevdata;
Lad, Prabhakar4b8a5312013-06-25 11:17:35 -03001258 int i, j = 0, err = 0;
Hans Verkuil01b1d9752012-09-20 09:06:28 -03001259 int res_idx = 0;
Chaithrika U Se7332e32009-06-09 05:55:37 -03001260 struct i2c_adapter *i2c_adap;
Chaithrika U Se7332e32009-06-09 05:55:37 -03001261 struct channel_obj *ch;
1262 struct video_device *vfd;
1263 struct resource *res;
1264 int subdev_count;
1265
1266 vpif_dev = &pdev->dev;
Muralidharan Karicheri317b2e22009-09-16 14:30:53 -03001267 err = initialize_vpif();
1268
1269 if (err) {
1270 v4l2_err(vpif_dev->driver, "Error initializing vpif\n");
1271 return err;
Chaithrika U Se7332e32009-06-09 05:55:37 -03001272 }
1273
Chaithrika U Se7332e32009-06-09 05:55:37 -03001274 err = v4l2_device_register(vpif_dev, &vpif_obj.v4l2_dev);
1275 if (err) {
1276 v4l2_err(vpif_dev->driver, "Error registering v4l2 device\n");
1277 return err;
1278 }
1279
Hans Verkuil01b1d9752012-09-20 09:06:28 -03001280 while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, res_idx))) {
Lad, Prabhakar379d2cf2013-06-17 11:20:51 -03001281 err = devm_request_irq(&pdev->dev, res->start, vpif_channel_isr,
Lad, Prabhakar76c4c2b2014-05-16 10:33:22 -03001282 IRQF_SHARED, VPIF_DRIVER_NAME,
Lad, Prabhakar379d2cf2013-06-17 11:20:51 -03001283 (void *)(&vpif_obj.dev[res_idx]->
1284 channel_id));
1285 if (err) {
1286 err = -EINVAL;
1287 vpif_err("VPIF IRQ request failed\n");
1288 goto vpif_unregister;
Chaithrika U Se7332e32009-06-09 05:55:37 -03001289 }
Hans Verkuil01b1d9752012-09-20 09:06:28 -03001290 res_idx++;
Chaithrika U Se7332e32009-06-09 05:55:37 -03001291 }
1292
1293 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
Chaithrika U Se7332e32009-06-09 05:55:37 -03001294 /* Get the pointer to the channel object */
1295 ch = vpif_obj.dev[i];
1296
1297 /* Allocate memory for video device */
1298 vfd = video_device_alloc();
1299 if (vfd == NULL) {
1300 for (j = 0; j < i; j++) {
1301 ch = vpif_obj.dev[j];
1302 video_device_release(ch->video_dev);
1303 }
1304 err = -ENOMEM;
Lad, Prabhakar9c63e012013-06-17 11:20:50 -03001305 goto vpif_unregister;
Chaithrika U Se7332e32009-06-09 05:55:37 -03001306 }
1307
Chaithrika U Se7332e32009-06-09 05:55:37 -03001308 /* Set video_dev to the video device */
1309 ch->video_dev = vfd;
1310 }
1311
Lad, Prabhakar4b8a5312013-06-25 11:17:35 -03001312 vpif_obj.config = pdev->dev.platform_data;
1313 subdev_count = vpif_obj.config->subdev_count;
1314 subdevdata = vpif_obj.config->subdevinfo;
Hans Verkuile6067f82012-09-20 09:06:27 -03001315 vpif_obj.sd = kzalloc(sizeof(struct v4l2_subdev *) * subdev_count,
1316 GFP_KERNEL);
1317 if (vpif_obj.sd == NULL) {
1318 vpif_err("unable to allocate memory for subdevice pointers\n");
1319 err = -ENOMEM;
Hans Verkuil01b1d9752012-09-20 09:06:28 -03001320 goto vpif_sd_error;
Hans Verkuile6067f82012-09-20 09:06:27 -03001321 }
1322
Lad, Prabhakar4b8a5312013-06-25 11:17:35 -03001323 if (!vpif_obj.config->asd_sizes) {
1324 i2c_adap = i2c_get_adapter(1);
1325 for (i = 0; i < subdev_count; i++) {
1326 vpif_obj.sd[i] =
1327 v4l2_i2c_new_subdev_board(&vpif_obj.v4l2_dev,
1328 i2c_adap,
1329 &subdevdata[i].
1330 board_info,
1331 NULL);
1332 if (!vpif_obj.sd[i]) {
1333 vpif_err("Error registering v4l2 subdevice\n");
Wei Yongjun9d3e9762013-08-22 22:59:44 -03001334 err = -ENODEV;
Lad, Prabhakar4b8a5312013-06-25 11:17:35 -03001335 goto probe_subdev_out;
1336 }
1337
1338 if (vpif_obj.sd[i])
1339 vpif_obj.sd[i]->grp_id = 1 << i;
1340 }
1341 vpif_probe_complete();
1342 } else {
Sylwester Nawrockie8419d02013-07-19 12:31:10 -03001343 vpif_obj.notifier.subdevs = vpif_obj.config->asd;
Lad, Prabhakar4b8a5312013-06-25 11:17:35 -03001344 vpif_obj.notifier.num_subdevs = vpif_obj.config->asd_sizes[0];
1345 vpif_obj.notifier.bound = vpif_async_bound;
1346 vpif_obj.notifier.complete = vpif_async_complete;
1347 err = v4l2_async_notifier_register(&vpif_obj.v4l2_dev,
1348 &vpif_obj.notifier);
1349 if (err) {
1350 vpif_err("Error registering async notifier\n");
1351 err = -EINVAL;
Hans Verkuile6067f82012-09-20 09:06:27 -03001352 goto probe_subdev_out;
1353 }
Hans Verkuile6067f82012-09-20 09:06:27 -03001354 }
1355
Chaithrika U Se7332e32009-06-09 05:55:37 -03001356 return 0;
1357
Hans Verkuile6067f82012-09-20 09:06:27 -03001358probe_subdev_out:
1359 kfree(vpif_obj.sd);
Hans Verkuil01b1d9752012-09-20 09:06:28 -03001360vpif_sd_error:
1361 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1362 ch = vpif_obj.dev[i];
1363 /* Note: does nothing if ch->video_dev == NULL */
1364 video_device_release(ch->video_dev);
1365 }
Lad, Prabhakar9c63e012013-06-17 11:20:50 -03001366vpif_unregister:
Chaithrika U Se7332e32009-06-09 05:55:37 -03001367 v4l2_device_unregister(&vpif_obj.v4l2_dev);
Chaithrika U Se7332e32009-06-09 05:55:37 -03001368
1369 return err;
1370}
1371
1372/*
1373 * vpif_remove: It un-register channels from V4L2 driver
1374 */
1375static int vpif_remove(struct platform_device *device)
1376{
Lad, Prabhakara2b235c2014-05-16 10:33:06 -03001377 struct common_obj *common;
Chaithrika U Se7332e32009-06-09 05:55:37 -03001378 struct channel_obj *ch;
Lad, Prabhakar9c63e012013-06-17 11:20:50 -03001379 int i;
Chaithrika U Se7332e32009-06-09 05:55:37 -03001380
1381 v4l2_device_unregister(&vpif_obj.v4l2_dev);
1382
Lad, Prabhakar0b361582013-06-17 11:20:48 -03001383 kfree(vpif_obj.sd);
Chaithrika U Se7332e32009-06-09 05:55:37 -03001384 /* un-register device */
1385 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1386 /* Get the pointer to the channel object */
1387 ch = vpif_obj.dev[i];
Prabhakar Lade9b58722014-07-18 13:31:51 -03001388 common = &ch->common[VPIF_VIDEO_INDEX];
Lad, Prabhakara2b235c2014-05-16 10:33:06 -03001389 vb2_dma_contig_cleanup_ctx(common->alloc_ctx);
Chaithrika U Se7332e32009-06-09 05:55:37 -03001390 /* Unregister video device */
1391 video_unregister_device(ch->video_dev);
1392
1393 ch->video_dev = NULL;
Lad, Prabhakar0b361582013-06-17 11:20:48 -03001394 kfree(vpif_obj.dev[i]);
Chaithrika U Se7332e32009-06-09 05:55:37 -03001395 }
1396
1397 return 0;
1398}
1399
Lad, Prabhakarba1902e2014-05-16 10:33:26 -03001400#ifdef CONFIG_PM_SLEEP
Manjunath Hadlie9530da2012-04-13 04:50:35 -03001401static int vpif_suspend(struct device *dev)
1402{
1403 struct common_obj *common;
1404 struct channel_obj *ch;
1405 int i;
1406
1407 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1408 /* Get the pointer to the channel object */
1409 ch = vpif_obj.dev[i];
1410 common = &ch->common[VPIF_VIDEO_INDEX];
Lad, Prabhakarba1902e2014-05-16 10:33:26 -03001411
1412 if (!vb2_is_streaming(&common->buffer_queue))
1413 continue;
1414
Manjunath Hadlie9530da2012-04-13 04:50:35 -03001415 mutex_lock(&common->lock);
Lad, Prabhakarba1902e2014-05-16 10:33:26 -03001416 /* Disable channel */
1417 if (ch->channel_id == VPIF_CHANNEL2_VIDEO) {
1418 enable_channel2(0);
1419 channel2_intr_enable(0);
1420 }
1421 if (ch->channel_id == VPIF_CHANNEL3_VIDEO ||
1422 ycmux_mode == 2) {
1423 enable_channel3(0);
1424 channel3_intr_enable(0);
Manjunath Hadlie9530da2012-04-13 04:50:35 -03001425 }
1426 mutex_unlock(&common->lock);
1427 }
1428
1429 return 0;
1430}
1431
1432static int vpif_resume(struct device *dev)
1433{
1434
1435 struct common_obj *common;
1436 struct channel_obj *ch;
1437 int i;
1438
1439 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1440 /* Get the pointer to the channel object */
1441 ch = vpif_obj.dev[i];
1442 common = &ch->common[VPIF_VIDEO_INDEX];
Lad, Prabhakarba1902e2014-05-16 10:33:26 -03001443
1444 if (!vb2_is_streaming(&common->buffer_queue))
1445 continue;
1446
Manjunath Hadlie9530da2012-04-13 04:50:35 -03001447 mutex_lock(&common->lock);
Lad, Prabhakarba1902e2014-05-16 10:33:26 -03001448 /* Enable channel */
1449 if (ch->channel_id == VPIF_CHANNEL2_VIDEO) {
1450 enable_channel2(1);
1451 channel2_intr_enable(1);
1452 }
1453 if (ch->channel_id == VPIF_CHANNEL3_VIDEO ||
1454 ycmux_mode == 2) {
1455 enable_channel3(1);
1456 channel3_intr_enable(1);
Manjunath Hadlie9530da2012-04-13 04:50:35 -03001457 }
1458 mutex_unlock(&common->lock);
1459 }
1460
1461 return 0;
1462}
1463
Manjunath Hadlie9530da2012-04-13 04:50:35 -03001464#endif
1465
Lad, Prabhakarba1902e2014-05-16 10:33:26 -03001466static SIMPLE_DEV_PM_OPS(vpif_pm_ops, vpif_suspend, vpif_resume);
1467
Mats Randgaardffa1b392010-08-30 10:30:36 -03001468static __refdata struct platform_driver vpif_driver = {
Chaithrika U Se7332e32009-06-09 05:55:37 -03001469 .driver = {
Lad, Prabhakar76c4c2b2014-05-16 10:33:22 -03001470 .name = VPIF_DRIVER_NAME,
Chaithrika U Se7332e32009-06-09 05:55:37 -03001471 .owner = THIS_MODULE,
Lad, Prabhakarba1902e2014-05-16 10:33:26 -03001472 .pm = &vpif_pm_ops,
Chaithrika U Se7332e32009-06-09 05:55:37 -03001473 },
1474 .probe = vpif_probe,
1475 .remove = vpif_remove,
1476};
1477
Lad, Prabhakarb8d067b2013-06-17 11:20:49 -03001478module_platform_driver(vpif_driver);