blob: 682e5d578bf7b843f73317b7a7c3a34bdf0fe5bd [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
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300200 addr = vb2_dma_contig_plane_dma_addr(&common->cur_frm->vb, 0);
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300201 common->set_addr((addr + common->ytop_off),
202 (addr + common->ybtm_off),
203 (addr + common->ctop_off),
204 (addr + common->cbtm_off));
205
Lad, Prabhakar58334b02014-05-16 10:33:13 -0300206 /*
207 * Set interrupt for both the fields in VPIF
208 * Register enable channel in VPIF register
209 */
Lad, Prabhakar9e184042012-09-14 10:22:24 -0300210 channel_first_int[VPIF_VIDEO_INDEX][ch->channel_id] = 1;
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300211 if (VPIF_CHANNEL2_VIDEO == ch->channel_id) {
212 channel2_intr_assert();
213 channel2_intr_enable(1);
214 enable_channel2(1);
Lad, Prabhakar2bd4e582012-09-25 08:11:49 -0300215 if (vpif_config_data->chan_config[VPIF_CHANNEL2_VIDEO].clip_en)
Manjunath Hadli6964b102012-06-29 03:20:12 -0300216 channel2_clipping_enable(1);
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300217 }
218
Lad, Prabhakar2f5851b2014-05-16 10:33:21 -0300219 if (VPIF_CHANNEL3_VIDEO == ch->channel_id || ycmux_mode == 2) {
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300220 channel3_intr_assert();
221 channel3_intr_enable(1);
222 enable_channel3(1);
Lad, Prabhakar2bd4e582012-09-25 08:11:49 -0300223 if (vpif_config_data->chan_config[VPIF_CHANNEL3_VIDEO].clip_en)
Manjunath Hadli6964b102012-06-29 03:20:12 -0300224 channel3_clipping_enable(1);
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300225 }
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300226
227 return 0;
Lad, Prabhakar54a445a42014-05-16 10:33:09 -0300228
229err:
230 list_for_each_entry_safe(buf, tmp, &common->dma_queue, list) {
231 list_del(&buf->list);
232 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_QUEUED);
233 }
Dan Carpenter92491962014-06-12 04:01:45 -0300234 spin_unlock_irqrestore(&common->irqlock, flags);
Lad, Prabhakar54a445a42014-05-16 10:33:09 -0300235
236 return ret;
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300237}
238
Lad, Prabhakar58334b02014-05-16 10:33:13 -0300239/**
240 * vpif_stop_streaming : Stop the DMA engine
241 * @vq: ptr to vb2_queue
242 *
243 * This callback stops the DMA engine and any remaining buffers
244 * in the DMA queue are released.
245 */
Hans Verkuile37559b2014-04-17 02:47:21 -0300246static void vpif_stop_streaming(struct vb2_queue *vq)
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300247{
Lad, Prabhakara2b235c2014-05-16 10:33:06 -0300248 struct channel_obj *ch = vb2_get_drv_priv(vq);
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300249 struct common_obj *common;
Hans Verkuilc4697d72012-11-16 12:03:07 -0300250 unsigned long flags;
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300251
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300252 common = &ch->common[VPIF_VIDEO_INDEX];
253
Lad, Prabhakar18c7adc2014-03-22 08:03:08 -0300254 /* Disable channel */
255 if (VPIF_CHANNEL2_VIDEO == ch->channel_id) {
256 enable_channel2(0);
257 channel2_intr_enable(0);
258 }
Lad, Prabhakar2f5851b2014-05-16 10:33:21 -0300259 if (VPIF_CHANNEL3_VIDEO == ch->channel_id || ycmux_mode == 2) {
Lad, Prabhakar18c7adc2014-03-22 08:03:08 -0300260 enable_channel3(0);
261 channel3_intr_enable(0);
262 }
Lad, Prabhakar18c7adc2014-03-22 08:03:08 -0300263
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300264 /* release all active buffers */
Hans Verkuilc4697d72012-11-16 12:03:07 -0300265 spin_lock_irqsave(&common->irqlock, flags);
Lad, Prabhakar18c7adc2014-03-22 08:03:08 -0300266 if (common->cur_frm == common->next_frm) {
267 vb2_buffer_done(&common->cur_frm->vb, VB2_BUF_STATE_ERROR);
268 } else {
269 if (common->cur_frm != NULL)
270 vb2_buffer_done(&common->cur_frm->vb,
271 VB2_BUF_STATE_ERROR);
272 if (common->next_frm != NULL)
273 vb2_buffer_done(&common->next_frm->vb,
274 VB2_BUF_STATE_ERROR);
275 }
276
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300277 while (!list_empty(&common->dma_queue)) {
278 common->next_frm = list_entry(common->dma_queue.next,
279 struct vpif_disp_buffer, list);
280 list_del(&common->next_frm->list);
281 vb2_buffer_done(&common->next_frm->vb, VB2_BUF_STATE_ERROR);
282 }
Hans Verkuilc4697d72012-11-16 12:03:07 -0300283 spin_unlock_irqrestore(&common->irqlock, flags);
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300284}
285
286static struct vb2_ops video_qops = {
287 .queue_setup = vpif_buffer_queue_setup,
Lad, Prabhakard10ed5c2014-05-16 10:33:08 -0300288 .wait_prepare = vb2_ops_wait_prepare,
289 .wait_finish = vb2_ops_wait_finish,
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300290 .buf_prepare = vpif_buffer_prepare,
291 .start_streaming = vpif_start_streaming,
292 .stop_streaming = vpif_stop_streaming,
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300293 .buf_queue = vpif_buffer_queue,
294};
295
Chaithrika U Se7332e32009-06-09 05:55:37 -0300296static void process_progressive_mode(struct common_obj *common)
297{
298 unsigned long addr = 0;
299
Hans Verkuilc4697d72012-11-16 12:03:07 -0300300 spin_lock(&common->irqlock);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300301 /* Get the next buffer from buffer queue */
302 common->next_frm = list_entry(common->dma_queue.next,
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300303 struct vpif_disp_buffer, list);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300304 /* Remove that buffer from the buffer queue */
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300305 list_del(&common->next_frm->list);
Hans Verkuilc4697d72012-11-16 12:03:07 -0300306 spin_unlock(&common->irqlock);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300307
308 /* Set top and bottom field addrs in VPIF registers */
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300309 addr = vb2_dma_contig_plane_dma_addr(&common->next_frm->vb, 0);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300310 common->set_addr(addr + common->ytop_off,
311 addr + common->ybtm_off,
312 addr + common->ctop_off,
313 addr + common->cbtm_off);
314}
315
316static void process_interlaced_mode(int fid, struct common_obj *common)
317{
318 /* device field id and local field id are in sync */
319 /* If this is even field */
320 if (0 == fid) {
321 if (common->cur_frm == common->next_frm)
322 return;
323
324 /* one frame is displayed If next frame is
325 * available, release cur_frm and move on */
326 /* Copy frame display time */
Sakari Ailus8e6057b2012-09-15 15:14:42 -0300327 v4l2_get_timestamp(&common->cur_frm->vb.v4l2_buf.timestamp);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300328 /* Change status of the cur_frm */
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300329 vb2_buffer_done(&common->cur_frm->vb,
330 VB2_BUF_STATE_DONE);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300331 /* Make cur_frm pointing to next_frm */
332 common->cur_frm = common->next_frm;
333
334 } else if (1 == fid) { /* odd field */
Hans Verkuilc4697d72012-11-16 12:03:07 -0300335 spin_lock(&common->irqlock);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300336 if (list_empty(&common->dma_queue)
337 || (common->cur_frm != common->next_frm)) {
Hans Verkuilc4697d72012-11-16 12:03:07 -0300338 spin_unlock(&common->irqlock);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300339 return;
340 }
Hans Verkuilc4697d72012-11-16 12:03:07 -0300341 spin_unlock(&common->irqlock);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300342 /* one field is displayed configure the next
343 * frame if it is available else hold on current
344 * frame */
345 /* Get next from the buffer queue */
346 process_progressive_mode(common);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300347 }
348}
349
350/*
351 * vpif_channel_isr: It changes status of the displayed buffer, takes next
352 * buffer from the queue and sets its address in VPIF registers
353 */
354static irqreturn_t vpif_channel_isr(int irq, void *dev_id)
355{
356 struct vpif_device *dev = &vpif_obj;
357 struct channel_obj *ch;
358 struct common_obj *common;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300359 int fid = -1, i;
360 int channel_id = 0;
361
362 channel_id = *(int *)(dev_id);
Manjunath Hadlib1fc4232012-04-13 04:43:10 -0300363 if (!vpif_intr_status(channel_id + 2))
364 return IRQ_NONE;
365
Chaithrika U Se7332e32009-06-09 05:55:37 -0300366 ch = dev->dev[channel_id];
Chaithrika U Se7332e32009-06-09 05:55:37 -0300367 for (i = 0; i < VPIF_NUMOBJECTS; i++) {
368 common = &ch->common[i];
369 /* If streaming is started in this channel */
Chaithrika U Se7332e32009-06-09 05:55:37 -0300370
371 if (1 == ch->vpifparams.std_info.frm_fmt) {
Hans Verkuilc4697d72012-11-16 12:03:07 -0300372 spin_lock(&common->irqlock);
373 if (list_empty(&common->dma_queue)) {
374 spin_unlock(&common->irqlock);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300375 continue;
Hans Verkuilc4697d72012-11-16 12:03:07 -0300376 }
377 spin_unlock(&common->irqlock);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300378
379 /* Progressive mode */
380 if (!channel_first_int[i][channel_id]) {
381 /* Mark status of the cur_frm to
382 * done and unlock semaphore on it */
Sakari Ailus8e6057b2012-09-15 15:14:42 -0300383 v4l2_get_timestamp(&common->cur_frm->vb.
384 v4l2_buf.timestamp);
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300385 vb2_buffer_done(&common->cur_frm->vb,
386 VB2_BUF_STATE_DONE);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300387 /* Make cur_frm pointing to next_frm */
388 common->cur_frm = common->next_frm;
389 }
390
391 channel_first_int[i][channel_id] = 0;
392 process_progressive_mode(common);
393 } else {
394 /* Interlaced mode */
395 /* If it is first interrupt, ignore it */
396
397 if (channel_first_int[i][channel_id]) {
398 channel_first_int[i][channel_id] = 0;
399 continue;
400 }
401
402 if (0 == i) {
403 ch->field_id ^= 1;
404 /* Get field id from VPIF registers */
405 fid = vpif_channel_getfid(ch->channel_id + 2);
406 /* If fid does not match with stored field id */
407 if (fid != ch->field_id) {
408 /* Make them in sync */
409 if (0 == fid)
410 ch->field_id = fid;
411
412 return IRQ_HANDLED;
413 }
414 }
415 process_interlaced_mode(fid, common);
416 }
417 }
418
419 return IRQ_HANDLED;
420}
421
Mats Randgaardc027e162010-12-16 12:17:44 -0300422static int vpif_update_std_info(struct channel_obj *ch)
Chaithrika U Se7332e32009-06-09 05:55:37 -0300423{
Chaithrika U Se7332e32009-06-09 05:55:37 -0300424 struct video_obj *vid_ch = &ch->video;
425 struct vpif_params *vpifparams = &ch->vpifparams;
426 struct vpif_channel_config_params *std_info = &vpifparams->std_info;
427 const struct vpif_channel_config_params *config;
428
Mats Randgaardc027e162010-12-16 12:17:44 -0300429 int i;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300430
Mats Randgaardc027e162010-12-16 12:17:44 -0300431 for (i = 0; i < vpif_ch_params_count; i++) {
Lad, Prabhakarced9b212013-03-20 01:28:27 -0300432 config = &vpif_ch_params[i];
Mats Randgaard40c8bce2010-12-16 12:17:43 -0300433 if (config->hd_sd == 0) {
434 vpif_dbg(2, debug, "SD format\n");
435 if (config->stdid & vid_ch->stdid) {
436 memcpy(std_info, config, sizeof(*config));
437 break;
438 }
Chaithrika U Se7332e32009-06-09 05:55:37 -0300439 }
440 }
441
Mats Randgaardc027e162010-12-16 12:17:44 -0300442 if (i == vpif_ch_params_count) {
443 vpif_dbg(1, debug, "Format not found\n");
Mats Randgaardaa444402010-12-16 12:17:42 -0300444 return -EINVAL;
Mats Randgaardc027e162010-12-16 12:17:44 -0300445 }
446
447 return 0;
448}
449
450static int vpif_update_resolution(struct channel_obj *ch)
451{
452 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
453 struct video_obj *vid_ch = &ch->video;
454 struct vpif_params *vpifparams = &ch->vpifparams;
455 struct vpif_channel_config_params *std_info = &vpifparams->std_info;
456
Hans Verkuil0598c172012-09-18 07:18:47 -0300457 if (!vid_ch->stdid && !vid_ch->dv_timings.bt.height)
Mats Randgaardc027e162010-12-16 12:17:44 -0300458 return -EINVAL;
459
Hans Verkuil0598c172012-09-18 07:18:47 -0300460 if (vid_ch->stdid) {
Mats Randgaardc027e162010-12-16 12:17:44 -0300461 if (vpif_update_std_info(ch))
462 return -EINVAL;
463 }
Chaithrika U Se7332e32009-06-09 05:55:37 -0300464
Lad, Prabhakar9cba6532014-05-16 10:33:30 -0300465 common->fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUV422P;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300466 common->fmt.fmt.pix.width = std_info->width;
467 common->fmt.fmt.pix.height = std_info->height;
468 vpif_dbg(1, debug, "Pixel details: Width = %d,Height = %d\n",
469 common->fmt.fmt.pix.width, common->fmt.fmt.pix.height);
470
471 /* Set height and width paramateres */
Mats Randgaardc027e162010-12-16 12:17:44 -0300472 common->height = std_info->height;
473 common->width = std_info->width;
Lad, Prabhakar9cba6532014-05-16 10:33:30 -0300474 common->fmt.fmt.pix.sizeimage = common->height * common->width * 2;
475
476 if (vid_ch->stdid)
477 common->fmt.fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
478 else
479 common->fmt.fmt.pix.colorspace = V4L2_COLORSPACE_REC709;
480
481 if (ch->vpifparams.std_info.frm_fmt)
482 common->fmt.fmt.pix.field = V4L2_FIELD_NONE;
483 else
484 common->fmt.fmt.pix.field = V4L2_FIELD_INTERLACED;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300485
486 return 0;
487}
488
489/*
490 * vpif_calculate_offsets: This function calculates buffers offset for Y and C
491 * in the top and bottom field
492 */
493static void vpif_calculate_offsets(struct channel_obj *ch)
494{
495 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
496 struct vpif_params *vpifparams = &ch->vpifparams;
497 enum v4l2_field field = common->fmt.fmt.pix.field;
498 struct video_obj *vid_ch = &ch->video;
Mauro Carvalho Chehaba4f20e22014-08-21 15:46:46 -0500499 unsigned int hpitch, sizeimage;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300500
501 if (V4L2_FIELD_ANY == common->fmt.fmt.pix.field) {
502 if (ch->vpifparams.std_info.frm_fmt)
503 vid_ch->buf_field = V4L2_FIELD_NONE;
504 else
505 vid_ch->buf_field = V4L2_FIELD_INTERLACED;
506 } else {
507 vid_ch->buf_field = common->fmt.fmt.pix.field;
508 }
509
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300510 sizeimage = common->fmt.fmt.pix.sizeimage;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300511
512 hpitch = common->fmt.fmt.pix.bytesperline;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300513 if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
514 (V4L2_FIELD_INTERLACED == vid_ch->buf_field)) {
515 common->ytop_off = 0;
516 common->ybtm_off = hpitch;
517 common->ctop_off = sizeimage / 2;
518 common->cbtm_off = sizeimage / 2 + hpitch;
519 } else if (V4L2_FIELD_SEQ_TB == vid_ch->buf_field) {
520 common->ytop_off = 0;
521 common->ybtm_off = sizeimage / 4;
522 common->ctop_off = sizeimage / 2;
523 common->cbtm_off = common->ctop_off + sizeimage / 4;
524 } else if (V4L2_FIELD_SEQ_BT == vid_ch->buf_field) {
525 common->ybtm_off = 0;
526 common->ytop_off = sizeimage / 4;
527 common->cbtm_off = sizeimage / 2;
528 common->ctop_off = common->cbtm_off + sizeimage / 4;
529 }
530
531 if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
532 (V4L2_FIELD_INTERLACED == vid_ch->buf_field)) {
533 vpifparams->video_params.storage_mode = 1;
534 } else {
535 vpifparams->video_params.storage_mode = 0;
536 }
537
538 if (ch->vpifparams.std_info.frm_fmt == 1) {
539 vpifparams->video_params.hpitch =
540 common->fmt.fmt.pix.bytesperline;
541 } else {
542 if ((field == V4L2_FIELD_ANY) ||
543 (field == V4L2_FIELD_INTERLACED))
544 vpifparams->video_params.hpitch =
545 common->fmt.fmt.pix.bytesperline * 2;
546 else
547 vpifparams->video_params.hpitch =
548 common->fmt.fmt.pix.bytesperline;
549 }
550
551 ch->vpifparams.video_params.stdid = ch->vpifparams.std_info.stdid;
552}
553
Chaithrika U Se7332e32009-06-09 05:55:37 -0300554static void vpif_config_addr(struct channel_obj *ch, int muxmode)
555{
556 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
557
558 if (VPIF_CHANNEL3_VIDEO == ch->channel_id) {
559 common->set_addr = ch3_set_videobuf_addr;
560 } else {
561 if (2 == muxmode)
562 common->set_addr = ch2_set_videobuf_addr_yc_nmux;
563 else
564 common->set_addr = ch2_set_videobuf_addr;
565 }
566}
567
Chaithrika U Se7332e32009-06-09 05:55:37 -0300568/* functions implementing ioctls */
Mats Randgaard2c0ddd12010-12-16 12:17:45 -0300569/**
570 * vpif_querycap() - QUERYCAP handler
571 * @file: file ptr
572 * @priv: file handle
573 * @cap: ptr to v4l2_capability structure
574 */
Chaithrika U Se7332e32009-06-09 05:55:37 -0300575static int vpif_querycap(struct file *file, void *priv,
576 struct v4l2_capability *cap)
577{
Muralidharan Karicheri317b2e22009-09-16 14:30:53 -0300578 struct vpif_display_config *config = vpif_dev->platform_data;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300579
Lad, Prabhakar626d533f2012-09-25 11:21:55 -0300580 cap->device_caps = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING;
581 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
Lad, Prabhakar76c4c2b2014-05-16 10:33:22 -0300582 strlcpy(cap->driver, VPIF_DRIVER_NAME, sizeof(cap->driver));
Lad, Prabhakar626d533f2012-09-25 11:21:55 -0300583 snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
584 dev_name(vpif_dev));
Chaithrika U Se7332e32009-06-09 05:55:37 -0300585 strlcpy(cap->card, config->card_name, sizeof(cap->card));
586
587 return 0;
588}
589
590static int vpif_enum_fmt_vid_out(struct file *file, void *priv,
591 struct v4l2_fmtdesc *fmt)
592{
Lad, Prabhakar9cba6532014-05-16 10:33:30 -0300593 if (fmt->index != 0)
Chaithrika U Se7332e32009-06-09 05:55:37 -0300594 return -EINVAL;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300595
596 /* Fill in the information about format */
597 fmt->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
598 strcpy(fmt->description, "YCbCr4:2:2 YC Planar");
599 fmt->pixelformat = V4L2_PIX_FMT_YUV422P;
Lad, Prabhakar9cba6532014-05-16 10:33:30 -0300600 fmt->flags = 0;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300601 return 0;
602}
603
604static int vpif_g_fmt_vid_out(struct file *file, void *priv,
605 struct v4l2_format *fmt)
606{
Lad, Prabhakarfcc22af2014-05-16 10:33:15 -0300607 struct video_device *vdev = video_devdata(file);
608 struct channel_obj *ch = video_get_drvdata(vdev);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300609 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
610
611 /* Check the validity of the buffer type */
612 if (common->fmt.type != fmt->type)
613 return -EINVAL;
614
Mats Randgaardc027e162010-12-16 12:17:44 -0300615 if (vpif_update_resolution(ch))
Hans Verkuil9bfaae22011-01-05 13:35:45 -0300616 return -EINVAL;
617 *fmt = common->fmt;
618 return 0;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300619}
620
Chaithrika U Se7332e32009-06-09 05:55:37 -0300621static int vpif_try_fmt_vid_out(struct file *file, void *priv,
622 struct v4l2_format *fmt)
623{
Lad, Prabhakarfcc22af2014-05-16 10:33:15 -0300624 struct video_device *vdev = video_devdata(file);
625 struct channel_obj *ch = video_get_drvdata(vdev);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300626 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
627 struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300628
Lad, Prabhakar9cba6532014-05-16 10:33:30 -0300629 /*
630 * to supress v4l-compliance warnings silently correct
631 * the pixelformat
632 */
633 if (pixfmt->pixelformat != V4L2_PIX_FMT_YUV422P)
634 pixfmt->pixelformat = common->fmt.fmt.pix.pixelformat;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300635
Lad, Prabhakar9cba6532014-05-16 10:33:30 -0300636 if (vpif_update_resolution(ch))
637 return -EINVAL;
638
639 pixfmt->colorspace = common->fmt.fmt.pix.colorspace;
640 pixfmt->field = common->fmt.fmt.pix.field;
641 pixfmt->bytesperline = common->fmt.fmt.pix.width;
642 pixfmt->width = common->fmt.fmt.pix.width;
643 pixfmt->height = common->fmt.fmt.pix.height;
644 pixfmt->sizeimage = pixfmt->bytesperline * pixfmt->height * 2;
Lad, Prabhakar9cba6532014-05-16 10:33:30 -0300645
646 return 0;
647}
648
649static int vpif_s_fmt_vid_out(struct file *file, void *priv,
650 struct v4l2_format *fmt)
651{
652 struct video_device *vdev = video_devdata(file);
653 struct channel_obj *ch = video_get_drvdata(vdev);
654 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
655 struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
656 int ret;
657
658 if (vb2_is_busy(&common->buffer_queue))
659 return -EBUSY;
660
661 ret = vpif_try_fmt_vid_out(file, priv, fmt);
662 if (ret)
663 return ret;
664
665 /* store the pix format in the channel object */
666 common->fmt.fmt.pix = *pixfmt;
667
668 /* store the format in the channel object */
669 common->fmt = *fmt;
670 return 0;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300671}
672
Hans Verkuil314527a2013-03-15 06:10:40 -0300673static int vpif_s_std(struct file *file, void *priv, v4l2_std_id std_id)
Chaithrika U Se7332e32009-06-09 05:55:37 -0300674{
Lad, Prabhakarf27d0f42014-05-16 10:33:28 -0300675 struct vpif_display_config *config = vpif_dev->platform_data;
Lad, Prabhakarfcc22af2014-05-16 10:33:15 -0300676 struct video_device *vdev = video_devdata(file);
677 struct channel_obj *ch = video_get_drvdata(vdev);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300678 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
Lad, Prabhakarf27d0f42014-05-16 10:33:28 -0300679 struct vpif_display_chan_config *chan_cfg;
680 struct v4l2_output output;
681 int ret;
682
683 if (config->chan_config[ch->channel_id].outputs == NULL)
684 return -ENODATA;
685
686 chan_cfg = &config->chan_config[ch->channel_id];
687 output = chan_cfg->outputs[ch->output_idx].output;
688 if (output.capabilities != V4L2_OUT_CAP_STD)
689 return -ENODATA;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300690
Lad, Prabhakar2f5851b2014-05-16 10:33:21 -0300691 if (vb2_is_busy(&common->buffer_queue))
692 return -EBUSY;
693
Lad, Prabhakarf27d0f42014-05-16 10:33:28 -0300694
Hans Verkuil314527a2013-03-15 06:10:40 -0300695 if (!(std_id & VPIF_V4L2_STD))
Chaithrika U Se7332e32009-06-09 05:55:37 -0300696 return -EINVAL;
697
Chaithrika U Se7332e32009-06-09 05:55:37 -0300698 /* Call encoder subdevice function to set the standard */
Hans Verkuil314527a2013-03-15 06:10:40 -0300699 ch->video.stdid = std_id;
Hans Verkuil0598c172012-09-18 07:18:47 -0300700 memset(&ch->video.dv_timings, 0, sizeof(ch->video.dv_timings));
Chaithrika U Se7332e32009-06-09 05:55:37 -0300701 /* Get the information about the standard */
Hans Verkuil9bfaae22011-01-05 13:35:45 -0300702 if (vpif_update_resolution(ch))
703 return -EINVAL;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300704
Chaithrika U Se7332e32009-06-09 05:55:37 -0300705 common->fmt.fmt.pix.bytesperline = common->fmt.fmt.pix.width;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300706
707 ret = v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 1, video,
Hans Verkuil314527a2013-03-15 06:10:40 -0300708 s_std_output, std_id);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300709 if (ret < 0) {
710 vpif_err("Failed to set output standard\n");
Hans Verkuil9bfaae22011-01-05 13:35:45 -0300711 return ret;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300712 }
713
Laurent Pinchart8774bed2014-04-28 16:53:01 -0300714 ret = v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 1, video,
Hans Verkuil314527a2013-03-15 06:10:40 -0300715 s_std, std_id);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300716 if (ret < 0)
717 vpif_err("Failed to set standard for sub devices\n");
Chaithrika U Se7332e32009-06-09 05:55:37 -0300718 return ret;
719}
720
721static int vpif_g_std(struct file *file, void *priv, v4l2_std_id *std)
722{
Lad, Prabhakarf27d0f42014-05-16 10:33:28 -0300723 struct vpif_display_config *config = vpif_dev->platform_data;
Lad, Prabhakarfcc22af2014-05-16 10:33:15 -0300724 struct video_device *vdev = video_devdata(file);
725 struct channel_obj *ch = video_get_drvdata(vdev);
Lad, Prabhakarf27d0f42014-05-16 10:33:28 -0300726 struct vpif_display_chan_config *chan_cfg;
727 struct v4l2_output output;
728
729 if (config->chan_config[ch->channel_id].outputs == NULL)
730 return -ENODATA;
731
732 chan_cfg = &config->chan_config[ch->channel_id];
733 output = chan_cfg->outputs[ch->output_idx].output;
734 if (output.capabilities != V4L2_OUT_CAP_STD)
735 return -ENODATA;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300736
737 *std = ch->video.stdid;
738 return 0;
739}
740
Chaithrika U Se7332e32009-06-09 05:55:37 -0300741static int vpif_enum_output(struct file *file, void *fh,
742 struct v4l2_output *output)
743{
744
Muralidharan Karicheri317b2e22009-09-16 14:30:53 -0300745 struct vpif_display_config *config = vpif_dev->platform_data;
Lad, Prabhakarfcc22af2014-05-16 10:33:15 -0300746 struct video_device *vdev = video_devdata(file);
747 struct channel_obj *ch = video_get_drvdata(vdev);
Lad, Prabhakar2bd4e582012-09-25 08:11:49 -0300748 struct vpif_display_chan_config *chan_cfg;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300749
Lad, Prabhakar2bd4e582012-09-25 08:11:49 -0300750 chan_cfg = &config->chan_config[ch->channel_id];
751 if (output->index >= chan_cfg->output_count) {
Chaithrika U Se7332e32009-06-09 05:55:37 -0300752 vpif_dbg(1, debug, "Invalid output index\n");
753 return -EINVAL;
754 }
755
Lad, Prabhakar2bd4e582012-09-25 08:11:49 -0300756 *output = chan_cfg->outputs[output->index].output;
757 return 0;
758}
Chaithrika U Se7332e32009-06-09 05:55:37 -0300759
Lad, Prabhakar2bd4e582012-09-25 08:11:49 -0300760/**
761 * vpif_output_to_subdev() - Maps output to sub device
762 * @vpif_cfg - global config ptr
763 * @chan_cfg - channel config ptr
764 * @index - Given output index from application
765 *
766 * lookup the sub device information for a given output index.
767 * we report all the output to application. output table also
768 * has sub device name for the each output
769 */
770static int
771vpif_output_to_subdev(struct vpif_display_config *vpif_cfg,
772 struct vpif_display_chan_config *chan_cfg, int index)
773{
774 struct vpif_subdev_info *subdev_info;
775 const char *subdev_name;
776 int i;
777
778 vpif_dbg(2, debug, "vpif_output_to_subdev\n");
779
780 if (chan_cfg->outputs == NULL)
781 return -1;
782
783 subdev_name = chan_cfg->outputs[index].subdev_name;
784 if (subdev_name == NULL)
785 return -1;
786
787 /* loop through the sub device list to get the sub device info */
788 for (i = 0; i < vpif_cfg->subdev_count; i++) {
789 subdev_info = &vpif_cfg->subdevinfo[i];
790 if (!strcmp(subdev_info->name, subdev_name))
791 return i;
792 }
793 return -1;
794}
795
796/**
797 * vpif_set_output() - Select an output
798 * @vpif_cfg - global config ptr
799 * @ch - channel
800 * @index - Given output index from application
801 *
802 * Select the given output.
803 */
804static int vpif_set_output(struct vpif_display_config *vpif_cfg,
805 struct channel_obj *ch, int index)
806{
807 struct vpif_display_chan_config *chan_cfg =
808 &vpif_cfg->chan_config[ch->channel_id];
Lad, Prabhakar2bd4e582012-09-25 08:11:49 -0300809 struct v4l2_subdev *sd = NULL;
810 u32 input = 0, output = 0;
811 int sd_index;
812 int ret;
813
814 sd_index = vpif_output_to_subdev(vpif_cfg, chan_cfg, index);
Mauro Carvalho Chehaba4f20e22014-08-21 15:46:46 -0500815 if (sd_index >= 0)
Lad, Prabhakar2bd4e582012-09-25 08:11:49 -0300816 sd = vpif_obj.sd[sd_index];
Lad, Prabhakar2bd4e582012-09-25 08:11:49 -0300817
818 if (sd) {
819 input = chan_cfg->outputs[index].input_route;
820 output = chan_cfg->outputs[index].output_route;
821 ret = v4l2_subdev_call(sd, video, s_routing, input, output, 0);
822 if (ret < 0 && ret != -ENOIOCTLCMD) {
823 vpif_err("Failed to set output\n");
824 return ret;
825 }
826
827 }
828 ch->output_idx = index;
829 ch->sd = sd;
830 if (chan_cfg->outputs != NULL)
831 /* update tvnorms from the sub device output info */
Lad, Prabhakare933c6b2015-03-08 18:57:24 -0300832 ch->video_dev.tvnorms = chan_cfg->outputs[index].output.std;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300833 return 0;
834}
835
836static int vpif_s_output(struct file *file, void *priv, unsigned int i)
837{
Lad, Prabhakar2bd4e582012-09-25 08:11:49 -0300838 struct vpif_display_config *config = vpif_dev->platform_data;
Lad, Prabhakarfcc22af2014-05-16 10:33:15 -0300839 struct video_device *vdev = video_devdata(file);
840 struct channel_obj *ch = video_get_drvdata(vdev);
Lad, Prabhakar2bd4e582012-09-25 08:11:49 -0300841 struct vpif_display_chan_config *chan_cfg;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300842 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
Lad, Prabhakar2bd4e582012-09-25 08:11:49 -0300843
Lad, Prabhakar2f5851b2014-05-16 10:33:21 -0300844 if (vb2_is_busy(&common->buffer_queue))
845 return -EBUSY;
846
Lad, Prabhakar2bd4e582012-09-25 08:11:49 -0300847 chan_cfg = &config->chan_config[ch->channel_id];
848
849 if (i >= chan_cfg->output_count)
850 return -EINVAL;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300851
Lad, Prabhakar2bd4e582012-09-25 08:11:49 -0300852 return vpif_set_output(config, ch, i);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300853}
854
855static int vpif_g_output(struct file *file, void *priv, unsigned int *i)
856{
Lad, Prabhakarfcc22af2014-05-16 10:33:15 -0300857 struct video_device *vdev = video_devdata(file);
858 struct channel_obj *ch = video_get_drvdata(vdev);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300859
Hans Verkuil311673e2012-09-20 09:06:23 -0300860 *i = ch->output_idx;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300861
862 return 0;
863}
864
Mats Randgaard40c8bce2010-12-16 12:17:43 -0300865/**
Hans Verkuil0598c172012-09-18 07:18:47 -0300866 * vpif_enum_dv_timings() - ENUM_DV_TIMINGS handler
Mats Randgaard40c8bce2010-12-16 12:17:43 -0300867 * @file: file ptr
868 * @priv: file handle
Hans Verkuil0598c172012-09-18 07:18:47 -0300869 * @timings: input timings
Mats Randgaard40c8bce2010-12-16 12:17:43 -0300870 */
Hans Verkuil0598c172012-09-18 07:18:47 -0300871static int
872vpif_enum_dv_timings(struct file *file, void *priv,
873 struct v4l2_enum_dv_timings *timings)
Mats Randgaard40c8bce2010-12-16 12:17:43 -0300874{
Lad, Prabhakar1093c592014-05-16 10:33:27 -0300875 struct vpif_display_config *config = vpif_dev->platform_data;
Lad, Prabhakarfcc22af2014-05-16 10:33:15 -0300876 struct video_device *vdev = video_devdata(file);
877 struct channel_obj *ch = video_get_drvdata(vdev);
Lad, Prabhakar1093c592014-05-16 10:33:27 -0300878 struct vpif_display_chan_config *chan_cfg;
879 struct v4l2_output output;
Lad, Prabhakar2bd4e582012-09-25 08:11:49 -0300880 int ret;
Mats Randgaard40c8bce2010-12-16 12:17:43 -0300881
Lad, Prabhakar1093c592014-05-16 10:33:27 -0300882 if (config->chan_config[ch->channel_id].outputs == NULL)
883 return -ENODATA;
884
885 chan_cfg = &config->chan_config[ch->channel_id];
886 output = chan_cfg->outputs[ch->output_idx].output;
887 if (output.capabilities != V4L2_OUT_CAP_DV_TIMINGS)
888 return -ENODATA;
889
Laurent Pinchart10d21462014-01-31 09:04:19 -0300890 timings->pad = 0;
891
892 ret = v4l2_subdev_call(ch->sd, pad, enum_dv_timings, timings);
Wei Yongjun63af4af2012-10-30 09:49:38 -0300893 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
Lad, Prabhakar2bd4e582012-09-25 08:11:49 -0300894 return -EINVAL;
895 return ret;
Mats Randgaard40c8bce2010-12-16 12:17:43 -0300896}
897
898/**
Mats Randgaardc027e162010-12-16 12:17:44 -0300899 * vpif_s_dv_timings() - S_DV_TIMINGS handler
900 * @file: file ptr
901 * @priv: file handle
902 * @timings: digital video timings
903 */
904static int vpif_s_dv_timings(struct file *file, void *priv,
905 struct v4l2_dv_timings *timings)
906{
Lad, Prabhakar1093c592014-05-16 10:33:27 -0300907 struct vpif_display_config *config = vpif_dev->platform_data;
Lad, Prabhakarfcc22af2014-05-16 10:33:15 -0300908 struct video_device *vdev = video_devdata(file);
909 struct channel_obj *ch = video_get_drvdata(vdev);
Mats Randgaardc027e162010-12-16 12:17:44 -0300910 struct vpif_params *vpifparams = &ch->vpifparams;
Lad, Prabhakar1093c592014-05-16 10:33:27 -0300911 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
Mats Randgaardc027e162010-12-16 12:17:44 -0300912 struct vpif_channel_config_params *std_info = &vpifparams->std_info;
913 struct video_obj *vid_ch = &ch->video;
Hans Verkuil0598c172012-09-18 07:18:47 -0300914 struct v4l2_bt_timings *bt = &vid_ch->dv_timings.bt;
Lad, Prabhakar1093c592014-05-16 10:33:27 -0300915 struct vpif_display_chan_config *chan_cfg;
916 struct v4l2_output output;
Mats Randgaardc027e162010-12-16 12:17:44 -0300917 int ret;
918
Lad, Prabhakar1093c592014-05-16 10:33:27 -0300919 if (config->chan_config[ch->channel_id].outputs == NULL)
920 return -ENODATA;
921
922 chan_cfg = &config->chan_config[ch->channel_id];
923 output = chan_cfg->outputs[ch->output_idx].output;
924 if (output.capabilities != V4L2_OUT_CAP_DV_TIMINGS)
925 return -ENODATA;
926
927 if (vb2_is_busy(&common->buffer_queue))
928 return -EBUSY;
929
Mats Randgaardc027e162010-12-16 12:17:44 -0300930 if (timings->type != V4L2_DV_BT_656_1120) {
931 vpif_dbg(2, debug, "Timing type not defined\n");
932 return -EINVAL;
933 }
934
935 /* Configure subdevice timings, if any */
Hans Verkuil882084a2012-09-20 09:06:31 -0300936 ret = v4l2_subdev_call(ch->sd, video, s_dv_timings, timings);
Lad, Prabhakar2bd4e582012-09-25 08:11:49 -0300937 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
938 ret = 0;
939 if (ret < 0) {
Mats Randgaardc027e162010-12-16 12:17:44 -0300940 vpif_dbg(2, debug, "Error setting custom DV timings\n");
941 return ret;
942 }
943
944 if (!(timings->bt.width && timings->bt.height &&
945 (timings->bt.hbackporch ||
946 timings->bt.hfrontporch ||
947 timings->bt.hsync) &&
948 timings->bt.vfrontporch &&
949 (timings->bt.vbackporch ||
950 timings->bt.vsync))) {
951 vpif_dbg(2, debug, "Timings for width, height, "
952 "horizontal back porch, horizontal sync, "
953 "horizontal front porch, vertical back porch, "
954 "vertical sync and vertical back porch "
955 "must be defined\n");
956 return -EINVAL;
957 }
958
Hans Verkuil0598c172012-09-18 07:18:47 -0300959 vid_ch->dv_timings = *timings;
Mats Randgaardc027e162010-12-16 12:17:44 -0300960
961 /* Configure video port timings */
962
Hans Verkuile3655262013-07-29 08:41:00 -0300963 std_info->eav2sav = V4L2_DV_BT_BLANKING_WIDTH(bt) - 8;
Mats Randgaardc027e162010-12-16 12:17:44 -0300964 std_info->sav2eav = bt->width;
965
966 std_info->l1 = 1;
967 std_info->l3 = bt->vsync + bt->vbackporch + 1;
968
Hans Verkuile3655262013-07-29 08:41:00 -0300969 std_info->vsize = V4L2_DV_BT_FRAME_HEIGHT(bt);
Mats Randgaardc027e162010-12-16 12:17:44 -0300970 if (bt->interlaced) {
971 if (bt->il_vbackporch || bt->il_vfrontporch || bt->il_vsync) {
Mats Randgaardc027e162010-12-16 12:17:44 -0300972 std_info->l5 = std_info->vsize/2 -
973 (bt->vfrontporch - 1);
974 std_info->l7 = std_info->vsize/2 + 1;
975 std_info->l9 = std_info->l7 + bt->il_vsync +
976 bt->il_vbackporch + 1;
977 std_info->l11 = std_info->vsize -
978 (bt->il_vfrontporch - 1);
979 } else {
980 vpif_dbg(2, debug, "Required timing values for "
981 "interlaced BT format missing\n");
982 return -EINVAL;
983 }
984 } else {
Mats Randgaardc027e162010-12-16 12:17:44 -0300985 std_info->l5 = std_info->vsize - (bt->vfrontporch - 1);
986 }
987 strncpy(std_info->name, "Custom timings BT656/1120",
988 VPIF_MAX_NAME);
989 std_info->width = bt->width;
990 std_info->height = bt->height;
991 std_info->frm_fmt = bt->interlaced ? 0 : 1;
992 std_info->ycmux_mode = 0;
993 std_info->capture_format = 0;
994 std_info->vbi_supported = 0;
995 std_info->hd_sd = 1;
996 std_info->stdid = 0;
Mats Randgaardc027e162010-12-16 12:17:44 -0300997 vid_ch->stdid = 0;
Mats Randgaardc027e162010-12-16 12:17:44 -0300998
999 return 0;
1000}
1001
1002/**
1003 * vpif_g_dv_timings() - G_DV_TIMINGS handler
1004 * @file: file ptr
1005 * @priv: file handle
1006 * @timings: digital video timings
1007 */
1008static int vpif_g_dv_timings(struct file *file, void *priv,
1009 struct v4l2_dv_timings *timings)
1010{
Lad, Prabhakar1093c592014-05-16 10:33:27 -03001011 struct vpif_display_config *config = vpif_dev->platform_data;
Lad, Prabhakarfcc22af2014-05-16 10:33:15 -03001012 struct video_device *vdev = video_devdata(file);
1013 struct channel_obj *ch = video_get_drvdata(vdev);
Lad, Prabhakar1093c592014-05-16 10:33:27 -03001014 struct vpif_display_chan_config *chan_cfg;
Mats Randgaardc027e162010-12-16 12:17:44 -03001015 struct video_obj *vid_ch = &ch->video;
Lad, Prabhakar1093c592014-05-16 10:33:27 -03001016 struct v4l2_output output;
1017
1018 if (config->chan_config[ch->channel_id].outputs == NULL)
1019 goto error;
1020
1021 chan_cfg = &config->chan_config[ch->channel_id];
1022 output = chan_cfg->outputs[ch->output_idx].output;
1023
1024 if (output.capabilities != V4L2_OUT_CAP_DV_TIMINGS)
1025 goto error;
Mats Randgaardc027e162010-12-16 12:17:44 -03001026
Hans Verkuil0598c172012-09-18 07:18:47 -03001027 *timings = vid_ch->dv_timings;
Mats Randgaardc027e162010-12-16 12:17:44 -03001028
1029 return 0;
Lad, Prabhakar1093c592014-05-16 10:33:27 -03001030error:
1031 return -ENODATA;
Mats Randgaardc027e162010-12-16 12:17:44 -03001032}
Mats Randgaard7036d6a2010-12-16 12:17:41 -03001033
1034/*
Mats Randgaard7036d6a2010-12-16 12:17:41 -03001035 * vpif_log_status() - Status information
1036 * @file: file ptr
1037 * @priv: file handle
1038 *
1039 * Returns zero.
1040 */
1041static int vpif_log_status(struct file *filep, void *priv)
1042{
1043 /* status for sub devices */
1044 v4l2_device_call_all(&vpif_obj.v4l2_dev, 0, core, log_status);
1045
1046 return 0;
1047}
1048
Chaithrika U Se7332e32009-06-09 05:55:37 -03001049/* vpif display ioctl operations */
1050static const struct v4l2_ioctl_ops vpif_ioctl_ops = {
Lad, Prabhakardcce8662014-05-16 10:33:25 -03001051 .vidioc_querycap = vpif_querycap,
Chaithrika U Se7332e32009-06-09 05:55:37 -03001052 .vidioc_enum_fmt_vid_out = vpif_enum_fmt_vid_out,
Lad, Prabhakardcce8662014-05-16 10:33:25 -03001053 .vidioc_g_fmt_vid_out = vpif_g_fmt_vid_out,
1054 .vidioc_s_fmt_vid_out = vpif_s_fmt_vid_out,
1055 .vidioc_try_fmt_vid_out = vpif_try_fmt_vid_out,
Lad, Prabhakarb92cde12014-05-16 10:33:16 -03001056
1057 .vidioc_reqbufs = vb2_ioctl_reqbufs,
1058 .vidioc_create_bufs = vb2_ioctl_create_bufs,
1059 .vidioc_querybuf = vb2_ioctl_querybuf,
1060 .vidioc_qbuf = vb2_ioctl_qbuf,
1061 .vidioc_dqbuf = vb2_ioctl_dqbuf,
1062 .vidioc_expbuf = vb2_ioctl_expbuf,
1063 .vidioc_streamon = vb2_ioctl_streamon,
1064 .vidioc_streamoff = vb2_ioctl_streamoff,
1065
Lad, Prabhakardcce8662014-05-16 10:33:25 -03001066 .vidioc_s_std = vpif_s_std,
Chaithrika U Se7332e32009-06-09 05:55:37 -03001067 .vidioc_g_std = vpif_g_std,
Lad, Prabhakardcce8662014-05-16 10:33:25 -03001068
Chaithrika U Se7332e32009-06-09 05:55:37 -03001069 .vidioc_enum_output = vpif_enum_output,
1070 .vidioc_s_output = vpif_s_output,
1071 .vidioc_g_output = vpif_g_output,
Lad, Prabhakardcce8662014-05-16 10:33:25 -03001072
1073 .vidioc_enum_dv_timings = vpif_enum_dv_timings,
1074 .vidioc_s_dv_timings = vpif_s_dv_timings,
1075 .vidioc_g_dv_timings = vpif_g_dv_timings,
1076
Mats Randgaard7036d6a2010-12-16 12:17:41 -03001077 .vidioc_log_status = vpif_log_status,
Chaithrika U Se7332e32009-06-09 05:55:37 -03001078};
1079
1080static const struct v4l2_file_operations vpif_fops = {
1081 .owner = THIS_MODULE,
Lad, Prabhakarfcc22af2014-05-16 10:33:15 -03001082 .open = v4l2_fh_open,
1083 .release = vb2_fop_release,
Hans Verkuil9bfaae22011-01-05 13:35:45 -03001084 .unlocked_ioctl = video_ioctl2,
Lad, Prabhakar4be21532014-05-16 10:33:14 -03001085 .mmap = vb2_fop_mmap,
1086 .poll = vb2_fop_poll
Chaithrika U Se7332e32009-06-09 05:55:37 -03001087};
1088
Chaithrika U Se7332e32009-06-09 05:55:37 -03001089/*Configure the channels, buffer sizei, request irq */
1090static int initialize_vpif(void)
1091{
1092 int free_channel_objects_index;
Lad, Prabhakard5b94b92014-05-16 10:33:23 -03001093 int err, i, j;
Chaithrika U Se7332e32009-06-09 05:55:37 -03001094
1095 /* Allocate memory for six channel objects */
1096 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1097 vpif_obj.dev[i] =
Mats Randgaard1f8766b2010-08-30 10:30:37 -03001098 kzalloc(sizeof(struct channel_obj), GFP_KERNEL);
Chaithrika U Se7332e32009-06-09 05:55:37 -03001099 /* If memory allocation fails, return error */
1100 if (!vpif_obj.dev[i]) {
1101 free_channel_objects_index = i;
1102 err = -ENOMEM;
1103 goto vpif_init_free_channel_objects;
1104 }
1105 }
1106
Chaithrika U Se7332e32009-06-09 05:55:37 -03001107 return 0;
1108
1109vpif_init_free_channel_objects:
1110 for (j = 0; j < free_channel_objects_index; j++)
1111 kfree(vpif_obj.dev[j]);
1112 return err;
1113}
1114
Lad, Prabhakar4b8a5312013-06-25 11:17:35 -03001115static int vpif_async_bound(struct v4l2_async_notifier *notifier,
1116 struct v4l2_subdev *subdev,
1117 struct v4l2_async_subdev *asd)
1118{
1119 int i;
1120
1121 for (i = 0; i < vpif_obj.config->subdev_count; i++)
1122 if (!strcmp(vpif_obj.config->subdevinfo[i].name,
1123 subdev->name)) {
1124 vpif_obj.sd[i] = subdev;
1125 vpif_obj.sd[i]->grp_id = 1 << i;
1126 return 0;
1127 }
1128
1129 return -EINVAL;
1130}
1131
1132static int vpif_probe_complete(void)
1133{
1134 struct common_obj *common;
Lad, Prabhakar4be21532014-05-16 10:33:14 -03001135 struct video_device *vdev;
Lad, Prabhakar4b8a5312013-06-25 11:17:35 -03001136 struct channel_obj *ch;
Lad, Prabhakara2b235c2014-05-16 10:33:06 -03001137 struct vb2_queue *q;
Lad, Prabhakar4b8a5312013-06-25 11:17:35 -03001138 int j, err, k;
1139
1140 for (j = 0; j < VPIF_DISPLAY_MAX_DEVICES; j++) {
1141 ch = vpif_obj.dev[j];
1142 /* Initialize field of the channel objects */
Lad, Prabhakar4b8a5312013-06-25 11:17:35 -03001143 for (k = 0; k < VPIF_NUMOBJECTS; k++) {
Lad, Prabhakar4b8a5312013-06-25 11:17:35 -03001144 common = &ch->common[k];
Lad, Prabhakar4b8a5312013-06-25 11:17:35 -03001145 spin_lock_init(&common->irqlock);
1146 mutex_init(&common->lock);
Lad, Prabhakar4b8a5312013-06-25 11:17:35 -03001147 common->set_addr = NULL;
1148 common->ytop_off = 0;
1149 common->ybtm_off = 0;
1150 common->ctop_off = 0;
1151 common->cbtm_off = 0;
1152 common->cur_frm = NULL;
1153 common->next_frm = NULL;
1154 memset(&common->fmt, 0, sizeof(common->fmt));
Lad, Prabhakar4b8a5312013-06-25 11:17:35 -03001155 }
1156 ch->initialized = 0;
1157 if (vpif_obj.config->subdev_count)
1158 ch->sd = vpif_obj.sd[0];
1159 ch->channel_id = j;
Lad, Prabhakar4b8a5312013-06-25 11:17:35 -03001160
1161 memset(&ch->vpifparams, 0, sizeof(ch->vpifparams));
1162
Lad, Prabhakar4b8a5312013-06-25 11:17:35 -03001163 ch->common[VPIF_VIDEO_INDEX].fmt.type =
1164 V4L2_BUF_TYPE_VIDEO_OUTPUT;
Lad, Prabhakar4b8a5312013-06-25 11:17:35 -03001165
1166 /* select output 0 */
1167 err = vpif_set_output(vpif_obj.config, ch, 0);
1168 if (err)
1169 goto probe_out;
1170
Lad, Prabhakar9cba6532014-05-16 10:33:30 -03001171 /* set initial format */
1172 ch->video.stdid = V4L2_STD_525_60;
1173 memset(&ch->video.dv_timings, 0, sizeof(ch->video.dv_timings));
1174 vpif_update_resolution(ch);
1175
Lad, Prabhakara2b235c2014-05-16 10:33:06 -03001176 /* Initialize vb2 queue */
1177 q = &common->buffer_queue;
1178 q->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
1179 q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
1180 q->drv_priv = ch;
1181 q->ops = &video_qops;
1182 q->mem_ops = &vb2_dma_contig_memops;
1183 q->buf_struct_size = sizeof(struct vpif_disp_buffer);
1184 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1185 q->min_buffers_needed = 1;
Lad, Prabhakard10ed5c2014-05-16 10:33:08 -03001186 q->lock = &common->lock;
Lad, Prabhakara2b235c2014-05-16 10:33:06 -03001187 err = vb2_queue_init(q);
1188 if (err) {
1189 vpif_err("vpif_display: vb2_queue_init() failed\n");
1190 goto probe_out;
1191 }
1192
1193 common->alloc_ctx = vb2_dma_contig_init_ctx(vpif_dev);
1194 if (IS_ERR(common->alloc_ctx)) {
1195 vpif_err("Failed to get the context\n");
1196 err = PTR_ERR(common->alloc_ctx);
1197 goto probe_out;
1198 }
1199
1200 INIT_LIST_HEAD(&common->dma_queue);
1201
Lad, Prabhakar4b8a5312013-06-25 11:17:35 -03001202 /* register video device */
Mauro Carvalho Chehab212bdba2014-08-22 06:38:14 -05001203 vpif_dbg(1, debug, "channel=%p,channel->video_dev=%p\n",
1204 ch, &ch->video_dev);
Lad, Prabhakar4b8a5312013-06-25 11:17:35 -03001205
Lad, Prabhakar76c4c2b2014-05-16 10:33:22 -03001206 /* Initialize the video_device structure */
Lad, Prabhakare933c6b2015-03-08 18:57:24 -03001207 vdev = &ch->video_dev;
Lad, Prabhakar76c4c2b2014-05-16 10:33:22 -03001208 strlcpy(vdev->name, VPIF_DRIVER_NAME, sizeof(vdev->name));
Lad, Prabhakare933c6b2015-03-08 18:57:24 -03001209 vdev->release = video_device_release_empty;
Lad, Prabhakar76c4c2b2014-05-16 10:33:22 -03001210 vdev->fops = &vpif_fops;
1211 vdev->ioctl_ops = &vpif_ioctl_ops;
1212 vdev->v4l2_dev = &vpif_obj.v4l2_dev;
1213 vdev->vfl_dir = VFL_DIR_TX;
Lad, Prabhakar4be21532014-05-16 10:33:14 -03001214 vdev->queue = q;
Lad, Prabhakarfcc22af2014-05-16 10:33:15 -03001215 vdev->lock = &common->lock;
Lad, Prabhakare933c6b2015-03-08 18:57:24 -03001216 video_set_drvdata(&ch->video_dev, ch);
Lad, Prabhakar4be21532014-05-16 10:33:14 -03001217 err = video_register_device(vdev, VFL_TYPE_GRABBER,
1218 (j ? 3 : 2));
Lad, Prabhakar4b8a5312013-06-25 11:17:35 -03001219 if (err < 0)
1220 goto probe_out;
1221 }
1222
1223 return 0;
1224
1225probe_out:
1226 for (k = 0; k < j; k++) {
1227 ch = vpif_obj.dev[k];
Lad, Prabhakara2b235c2014-05-16 10:33:06 -03001228 common = &ch->common[k];
1229 vb2_dma_contig_cleanup_ctx(common->alloc_ctx);
Lad, Prabhakare933c6b2015-03-08 18:57:24 -03001230 video_unregister_device(&ch->video_dev);
Lad, Prabhakar4b8a5312013-06-25 11:17:35 -03001231 }
1232 return err;
1233}
1234
1235static int vpif_async_complete(struct v4l2_async_notifier *notifier)
1236{
1237 return vpif_probe_complete();
1238}
1239
Chaithrika U Se7332e32009-06-09 05:55:37 -03001240/*
1241 * vpif_probe: This function creates device entries by register itself to the
1242 * V4L2 driver and initializes fields of each channel objects
1243 */
1244static __init int vpif_probe(struct platform_device *pdev)
1245{
Muralidharan Karicheri317b2e22009-09-16 14:30:53 -03001246 struct vpif_subdev_info *subdevdata;
Chaithrika U Se7332e32009-06-09 05:55:37 -03001247 struct i2c_adapter *i2c_adap;
Chaithrika U Se7332e32009-06-09 05:55:37 -03001248 struct resource *res;
1249 int subdev_count;
Lad, Prabhakare933c6b2015-03-08 18:57:24 -03001250 int res_idx = 0;
1251 int i, err;
Chaithrika U Se7332e32009-06-09 05:55:37 -03001252
1253 vpif_dev = &pdev->dev;
Muralidharan Karicheri317b2e22009-09-16 14:30:53 -03001254 err = initialize_vpif();
1255
1256 if (err) {
1257 v4l2_err(vpif_dev->driver, "Error initializing vpif\n");
1258 return err;
Chaithrika U Se7332e32009-06-09 05:55:37 -03001259 }
1260
Chaithrika U Se7332e32009-06-09 05:55:37 -03001261 err = v4l2_device_register(vpif_dev, &vpif_obj.v4l2_dev);
1262 if (err) {
1263 v4l2_err(vpif_dev->driver, "Error registering v4l2 device\n");
1264 return err;
1265 }
1266
Hans Verkuil01b1d9752012-09-20 09:06:28 -03001267 while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, res_idx))) {
Lad, Prabhakar379d2cf2013-06-17 11:20:51 -03001268 err = devm_request_irq(&pdev->dev, res->start, vpif_channel_isr,
Lad, Prabhakar76c4c2b2014-05-16 10:33:22 -03001269 IRQF_SHARED, VPIF_DRIVER_NAME,
Lad, Prabhakar379d2cf2013-06-17 11:20:51 -03001270 (void *)(&vpif_obj.dev[res_idx]->
1271 channel_id));
1272 if (err) {
1273 err = -EINVAL;
1274 vpif_err("VPIF IRQ request failed\n");
1275 goto vpif_unregister;
Chaithrika U Se7332e32009-06-09 05:55:37 -03001276 }
Hans Verkuil01b1d9752012-09-20 09:06:28 -03001277 res_idx++;
Chaithrika U Se7332e32009-06-09 05:55:37 -03001278 }
1279
Lad, Prabhakar4b8a5312013-06-25 11:17:35 -03001280 vpif_obj.config = pdev->dev.platform_data;
1281 subdev_count = vpif_obj.config->subdev_count;
1282 subdevdata = vpif_obj.config->subdevinfo;
Hans Verkuile6067f82012-09-20 09:06:27 -03001283 vpif_obj.sd = kzalloc(sizeof(struct v4l2_subdev *) * subdev_count,
1284 GFP_KERNEL);
1285 if (vpif_obj.sd == NULL) {
1286 vpif_err("unable to allocate memory for subdevice pointers\n");
1287 err = -ENOMEM;
Lad, Prabhakare933c6b2015-03-08 18:57:24 -03001288 goto vpif_unregister;
Hans Verkuile6067f82012-09-20 09:06:27 -03001289 }
1290
Lad, Prabhakar4b8a5312013-06-25 11:17:35 -03001291 if (!vpif_obj.config->asd_sizes) {
1292 i2c_adap = i2c_get_adapter(1);
1293 for (i = 0; i < subdev_count; i++) {
1294 vpif_obj.sd[i] =
1295 v4l2_i2c_new_subdev_board(&vpif_obj.v4l2_dev,
1296 i2c_adap,
1297 &subdevdata[i].
1298 board_info,
1299 NULL);
1300 if (!vpif_obj.sd[i]) {
1301 vpif_err("Error registering v4l2 subdevice\n");
Wei Yongjun9d3e9762013-08-22 22:59:44 -03001302 err = -ENODEV;
Lad, Prabhakar4b8a5312013-06-25 11:17:35 -03001303 goto probe_subdev_out;
1304 }
1305
1306 if (vpif_obj.sd[i])
1307 vpif_obj.sd[i]->grp_id = 1 << i;
1308 }
1309 vpif_probe_complete();
1310 } else {
Sylwester Nawrockie8419d02013-07-19 12:31:10 -03001311 vpif_obj.notifier.subdevs = vpif_obj.config->asd;
Lad, Prabhakar4b8a5312013-06-25 11:17:35 -03001312 vpif_obj.notifier.num_subdevs = vpif_obj.config->asd_sizes[0];
1313 vpif_obj.notifier.bound = vpif_async_bound;
1314 vpif_obj.notifier.complete = vpif_async_complete;
1315 err = v4l2_async_notifier_register(&vpif_obj.v4l2_dev,
1316 &vpif_obj.notifier);
1317 if (err) {
1318 vpif_err("Error registering async notifier\n");
1319 err = -EINVAL;
Hans Verkuile6067f82012-09-20 09:06:27 -03001320 goto probe_subdev_out;
1321 }
Hans Verkuile6067f82012-09-20 09:06:27 -03001322 }
1323
Chaithrika U Se7332e32009-06-09 05:55:37 -03001324 return 0;
1325
Hans Verkuile6067f82012-09-20 09:06:27 -03001326probe_subdev_out:
1327 kfree(vpif_obj.sd);
Lad, Prabhakar9c63e012013-06-17 11:20:50 -03001328vpif_unregister:
Chaithrika U Se7332e32009-06-09 05:55:37 -03001329 v4l2_device_unregister(&vpif_obj.v4l2_dev);
Chaithrika U Se7332e32009-06-09 05:55:37 -03001330
1331 return err;
1332}
1333
1334/*
1335 * vpif_remove: It un-register channels from V4L2 driver
1336 */
1337static int vpif_remove(struct platform_device *device)
1338{
Lad, Prabhakara2b235c2014-05-16 10:33:06 -03001339 struct common_obj *common;
Chaithrika U Se7332e32009-06-09 05:55:37 -03001340 struct channel_obj *ch;
Lad, Prabhakar9c63e012013-06-17 11:20:50 -03001341 int i;
Chaithrika U Se7332e32009-06-09 05:55:37 -03001342
1343 v4l2_device_unregister(&vpif_obj.v4l2_dev);
1344
Lad, Prabhakar0b361582013-06-17 11:20:48 -03001345 kfree(vpif_obj.sd);
Chaithrika U Se7332e32009-06-09 05:55:37 -03001346 /* un-register device */
1347 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1348 /* Get the pointer to the channel object */
1349 ch = vpif_obj.dev[i];
Prabhakar Lade9b58722014-07-18 13:31:51 -03001350 common = &ch->common[VPIF_VIDEO_INDEX];
Lad, Prabhakara2b235c2014-05-16 10:33:06 -03001351 vb2_dma_contig_cleanup_ctx(common->alloc_ctx);
Chaithrika U Se7332e32009-06-09 05:55:37 -03001352 /* Unregister video device */
Lad, Prabhakare933c6b2015-03-08 18:57:24 -03001353 video_unregister_device(&ch->video_dev);
Lad, Prabhakar0b361582013-06-17 11:20:48 -03001354 kfree(vpif_obj.dev[i]);
Chaithrika U Se7332e32009-06-09 05:55:37 -03001355 }
1356
1357 return 0;
1358}
1359
Lad, Prabhakarba1902e2014-05-16 10:33:26 -03001360#ifdef CONFIG_PM_SLEEP
Manjunath Hadlie9530da2012-04-13 04:50:35 -03001361static int vpif_suspend(struct device *dev)
1362{
1363 struct common_obj *common;
1364 struct channel_obj *ch;
1365 int i;
1366
1367 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1368 /* Get the pointer to the channel object */
1369 ch = vpif_obj.dev[i];
1370 common = &ch->common[VPIF_VIDEO_INDEX];
Lad, Prabhakarba1902e2014-05-16 10:33:26 -03001371
Prabhakar Lad81578922014-09-06 12:26:50 -03001372 if (!vb2_start_streaming_called(&common->buffer_queue))
Lad, Prabhakarba1902e2014-05-16 10:33:26 -03001373 continue;
1374
Manjunath Hadlie9530da2012-04-13 04:50:35 -03001375 mutex_lock(&common->lock);
Lad, Prabhakarba1902e2014-05-16 10:33:26 -03001376 /* Disable channel */
1377 if (ch->channel_id == VPIF_CHANNEL2_VIDEO) {
1378 enable_channel2(0);
1379 channel2_intr_enable(0);
1380 }
1381 if (ch->channel_id == VPIF_CHANNEL3_VIDEO ||
1382 ycmux_mode == 2) {
1383 enable_channel3(0);
1384 channel3_intr_enable(0);
Manjunath Hadlie9530da2012-04-13 04:50:35 -03001385 }
1386 mutex_unlock(&common->lock);
1387 }
1388
1389 return 0;
1390}
1391
1392static int vpif_resume(struct device *dev)
1393{
1394
1395 struct common_obj *common;
1396 struct channel_obj *ch;
1397 int i;
1398
1399 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1400 /* Get the pointer to the channel object */
1401 ch = vpif_obj.dev[i];
1402 common = &ch->common[VPIF_VIDEO_INDEX];
Lad, Prabhakarba1902e2014-05-16 10:33:26 -03001403
Prabhakar Lad81578922014-09-06 12:26:50 -03001404 if (!vb2_start_streaming_called(&common->buffer_queue))
Lad, Prabhakarba1902e2014-05-16 10:33:26 -03001405 continue;
1406
Manjunath Hadlie9530da2012-04-13 04:50:35 -03001407 mutex_lock(&common->lock);
Lad, Prabhakarba1902e2014-05-16 10:33:26 -03001408 /* Enable channel */
1409 if (ch->channel_id == VPIF_CHANNEL2_VIDEO) {
1410 enable_channel2(1);
1411 channel2_intr_enable(1);
1412 }
1413 if (ch->channel_id == VPIF_CHANNEL3_VIDEO ||
1414 ycmux_mode == 2) {
1415 enable_channel3(1);
1416 channel3_intr_enable(1);
Manjunath Hadlie9530da2012-04-13 04:50:35 -03001417 }
1418 mutex_unlock(&common->lock);
1419 }
1420
1421 return 0;
1422}
1423
Manjunath Hadlie9530da2012-04-13 04:50:35 -03001424#endif
1425
Lad, Prabhakarba1902e2014-05-16 10:33:26 -03001426static SIMPLE_DEV_PM_OPS(vpif_pm_ops, vpif_suspend, vpif_resume);
1427
Mats Randgaardffa1b392010-08-30 10:30:36 -03001428static __refdata struct platform_driver vpif_driver = {
Chaithrika U Se7332e32009-06-09 05:55:37 -03001429 .driver = {
Lad, Prabhakar76c4c2b2014-05-16 10:33:22 -03001430 .name = VPIF_DRIVER_NAME,
Lad, Prabhakarba1902e2014-05-16 10:33:26 -03001431 .pm = &vpif_pm_ops,
Chaithrika U Se7332e32009-06-09 05:55:37 -03001432 },
1433 .probe = vpif_probe,
1434 .remove = vpif_remove,
1435};
1436
Lad, Prabhakarb8d067b2013-06-17 11:20:49 -03001437module_platform_driver(vpif_driver);