blob: 27357954f1bbf29c5257199b7be194d7cc8845c6 [file] [log] [blame]
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001/*
2 * Copyright (C) 2009 Texas Instruments Inc
Lad, Prabhakar96787eb2014-05-16 10:33:55 -03003 * Copyright (C) 2014 Lad, Prabhakar <prabhakar.csengg@gmail.com>
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03004 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030015 * TODO : add support for VBI & HBI data service
16 * add static buffer allocation
17 */
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030018
Lad, Prabhakar012eef72013-04-19 05:53:29 -030019#include <linux/module.h>
20#include <linux/interrupt.h>
21#include <linux/platform_device.h>
22#include <linux/slab.h>
23
Lad, Prabhakar012eef72013-04-19 05:53:29 -030024#include <media/v4l2-ioctl.h>
Kevin Hilman4a5f8ae2017-06-06 20:37:39 -030025#include <media/v4l2-of.h>
26#include <media/i2c/tvp514x.h>
Kevin Hilmandde32d42017-06-06 20:37:40 -030027#include <media/v4l2-mediabus.h>
28
29#include <linux/videodev2.h>
Lad, Prabhakar012eef72013-04-19 05:53:29 -030030
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030031#include "vpif.h"
Lad, Prabhakar012eef72013-04-19 05:53:29 -030032#include "vpif_capture.h"
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030033
34MODULE_DESCRIPTION("TI DaVinci VPIF Capture driver");
35MODULE_LICENSE("GPL");
Mauro Carvalho Chehab64dc3c12011-06-25 11:28:37 -030036MODULE_VERSION(VPIF_CAPTURE_VERSION);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030037
38#define vpif_err(fmt, arg...) v4l2_err(&vpif_obj.v4l2_dev, fmt, ## arg)
39#define vpif_dbg(level, debug, fmt, arg...) \
40 v4l2_dbg(level, debug, &vpif_obj.v4l2_dev, fmt, ## arg)
41
42static int debug = 1;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030043
44module_param(debug, int, 0644);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030045
46MODULE_PARM_DESC(debug, "Debug level 0-1");
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030047
Lad, Prabhakare75ea0c2014-05-16 10:33:46 -030048#define VPIF_DRIVER_NAME "vpif_capture"
Kevin Hilmanbff782d2016-12-16 22:47:54 -020049MODULE_ALIAS("platform:" VPIF_DRIVER_NAME);
Lad, Prabhakare75ea0c2014-05-16 10:33:46 -030050
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030051/* global variables */
52static struct vpif_device vpif_obj = { {NULL} };
53static struct device *vpif_dev;
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -030054static void vpif_calculate_offsets(struct channel_obj *ch);
55static void vpif_config_addr(struct channel_obj *ch, int muxmode);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030056
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -030057static u8 channel_first_int[VPIF_NUMBER_OF_OBJECTS][2] = { {1, 1} };
58
Lad, Prabhakarc66238f2014-05-16 10:33:45 -030059/* Is set to 1 in case of SDTV formats, 2 in case of HDTV formats. */
60static int ycmux_mode;
61
Junghak Sung2d700712015-09-22 10:30:30 -030062static inline
63struct vpif_cap_buffer *to_vpif_buffer(struct vb2_v4l2_buffer *vb)
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -030064{
65 return container_of(vb, struct vpif_cap_buffer, vb);
66}
67
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030068/**
Lad, Prabhakar23e76a22014-05-16 10:33:37 -030069 * vpif_buffer_prepare : callback function for buffer prepare
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -030070 * @vb: ptr to vb2_buffer
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030071 *
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -030072 * This is the callback function for buffer prepare when vb2_qbuf()
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030073 * function is called. The buffer is prepared and user space virtual address
74 * or user address is converted into physical address
75 */
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -030076static int vpif_buffer_prepare(struct vb2_buffer *vb)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030077{
Junghak Sung2d700712015-09-22 10:30:30 -030078 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -030079 struct vb2_queue *q = vb->vb2_queue;
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -030080 struct channel_obj *ch = vb2_get_drv_priv(q);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030081 struct common_obj *common;
82 unsigned long addr;
83
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030084 vpif_dbg(2, debug, "vpif_buffer_prepare\n");
85
86 common = &ch->common[VPIF_VIDEO_INDEX];
87
Lad, Prabhakar23e76a22014-05-16 10:33:37 -030088 vb2_set_plane_payload(vb, 0, common->fmt.fmt.pix.sizeimage);
89 if (vb2_get_plane_payload(vb, 0) > vb2_plane_size(vb, 0))
90 return -EINVAL;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030091
Junghak Sung2d700712015-09-22 10:30:30 -030092 vbuf->field = common->fmt.fmt.pix.field;
Lad, Prabhakar23e76a22014-05-16 10:33:37 -030093
94 addr = vb2_dma_contig_plane_dma_addr(vb, 0);
95 if (!IS_ALIGNED((addr + common->ytop_off), 8) ||
96 !IS_ALIGNED((addr + common->ybtm_off), 8) ||
97 !IS_ALIGNED((addr + common->ctop_off), 8) ||
98 !IS_ALIGNED((addr + common->cbtm_off), 8)) {
99 vpif_dbg(1, debug, "offset is not aligned\n");
100 return -EINVAL;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300101 }
Lad, Prabhakar23e76a22014-05-16 10:33:37 -0300102
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300103 return 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300104}
105
106/**
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300107 * vpif_buffer_queue_setup : Callback function for buffer setup.
108 * @vq: vb2_queue ptr
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300109 * @nbuffers: ptr to number of buffers requested by application
110 * @nplanes:: contains number of distinct video planes needed to hold a frame
111 * @sizes[]: contains the size (in bytes) of each plane.
Hans Verkuil36c0f8b2016-04-15 09:15:05 -0300112 * @alloc_devs: ptr to allocation context
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300113 *
114 * This callback function is called when reqbuf() is called to adjust
115 * the buffer count and buffer size
116 */
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300117static int vpif_buffer_queue_setup(struct vb2_queue *vq,
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300118 unsigned int *nbuffers, unsigned int *nplanes,
Hans Verkuil36c0f8b2016-04-15 09:15:05 -0300119 unsigned int sizes[], struct device *alloc_devs[])
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300120{
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -0300121 struct channel_obj *ch = vb2_get_drv_priv(vq);
Hans Verkuildf9ecb02015-10-28 00:50:37 -0200122 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
123 unsigned size = common->fmt.fmt.pix.sizeimage;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300124
125 vpif_dbg(2, debug, "vpif_buffer_setup\n");
126
Hans Verkuildf9ecb02015-10-28 00:50:37 -0200127 if (*nplanes) {
128 if (sizes[0] < size)
129 return -EINVAL;
130 size = sizes[0];
131 }
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300132
Lad, Prabhakar837939d2014-05-16 10:33:38 -0300133 if (vq->num_buffers + *nbuffers < 3)
134 *nbuffers = 3 - vq->num_buffers;
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300135
136 *nplanes = 1;
Hans Verkuildf9ecb02015-10-28 00:50:37 -0200137 sizes[0] = size;
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300138
Lad, Prabhakar837939d2014-05-16 10:33:38 -0300139 /* Calculate the offset for Y and C data in the buffer */
140 vpif_calculate_offsets(ch);
141
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300142 return 0;
143}
144
145/**
146 * vpif_buffer_queue : Callback function to add buffer to DMA queue
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300147 * @vb: ptr to vb2_buffer
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300148 */
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300149static void vpif_buffer_queue(struct vb2_buffer *vb)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300150{
Junghak Sung2d700712015-09-22 10:30:30 -0300151 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -0300152 struct channel_obj *ch = vb2_get_drv_priv(vb->vb2_queue);
Junghak Sung2d700712015-09-22 10:30:30 -0300153 struct vpif_cap_buffer *buf = to_vpif_buffer(vbuf);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300154 struct common_obj *common;
Hans Verkuilaec96832012-11-16 12:03:06 -0300155 unsigned long flags;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300156
157 common = &ch->common[VPIF_VIDEO_INDEX];
158
159 vpif_dbg(2, debug, "vpif_buffer_queue\n");
160
Hans Verkuilaec96832012-11-16 12:03:06 -0300161 spin_lock_irqsave(&common->irqlock, flags);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300162 /* add the buffer to the DMA queue */
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300163 list_add_tail(&buf->list, &common->dma_queue);
Hans Verkuilaec96832012-11-16 12:03:06 -0300164 spin_unlock_irqrestore(&common->irqlock, flags);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300165}
166
Lad, Prabhakar41b9f242014-05-16 10:33:39 -0300167/**
168 * vpif_start_streaming : Starts the DMA engine for streaming
169 * @vb: ptr to vb2_buffer
170 * @count: number of buffers
171 */
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300172static int vpif_start_streaming(struct vb2_queue *vq, unsigned int count)
173{
174 struct vpif_capture_config *vpif_config_data =
175 vpif_dev->platform_data;
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -0300176 struct channel_obj *ch = vb2_get_drv_priv(vq);
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300177 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
178 struct vpif_params *vpif = &ch->vpifparams;
Lad, Prabhakarbebd8d12014-05-16 10:33:35 -0300179 struct vpif_cap_buffer *buf, *tmp;
180 unsigned long addr, flags;
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300181 int ret;
182
Lad, Prabhakarc66238f2014-05-16 10:33:45 -0300183 /* Initialize field_id */
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300184 ch->field_id = 0;
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300185
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300186 /* configure 1 or 2 channel mode */
Lad, Prabhakarf4ad8d72012-09-27 02:33:12 -0300187 if (vpif_config_data->setup_input_channel_mode) {
188 ret = vpif_config_data->
189 setup_input_channel_mode(vpif->std_info.ycmux_mode);
190 if (ret < 0) {
191 vpif_dbg(1, debug, "can't set vpif channel mode\n");
Lad, Prabhakarbebd8d12014-05-16 10:33:35 -0300192 goto err;
Lad, Prabhakarf4ad8d72012-09-27 02:33:12 -0300193 }
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300194 }
195
Lad, Prabhakard9a3b132014-05-16 10:33:42 -0300196 ret = v4l2_subdev_call(ch->sd, video, s_stream, 1);
197 if (ret && ret != -ENOIOCTLCMD && ret != -ENODEV) {
198 vpif_dbg(1, debug, "stream on failed in subdev\n");
199 goto err;
200 }
201
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300202 /* Call vpif_set_params function to set the parameters and addresses */
203 ret = vpif_set_video_params(vpif, ch->channel_id);
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300204 if (ret < 0) {
205 vpif_dbg(1, debug, "can't set video params\n");
Lad, Prabhakarbebd8d12014-05-16 10:33:35 -0300206 goto err;
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300207 }
208
Lad, Prabhakarc66238f2014-05-16 10:33:45 -0300209 ycmux_mode = ret;
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300210 vpif_config_addr(ch, ret);
211
Lad, Prabhakarbebd8d12014-05-16 10:33:35 -0300212 /* Get the next frame from the buffer queue */
Kevin Hilman62dd4ac2016-12-07 16:30:23 -0200213 spin_lock_irqsave(&common->irqlock, flags);
Lad, Prabhakarbebd8d12014-05-16 10:33:35 -0300214 common->cur_frm = common->next_frm = list_entry(common->dma_queue.next,
215 struct vpif_cap_buffer, list);
216 /* Remove buffer from the buffer queue */
217 list_del(&common->cur_frm->list);
218 spin_unlock_irqrestore(&common->irqlock, flags);
Lad, Prabhakarbebd8d12014-05-16 10:33:35 -0300219
Junghak Sung2d700712015-09-22 10:30:30 -0300220 addr = vb2_dma_contig_plane_dma_addr(&common->cur_frm->vb.vb2_buf, 0);
Lad, Prabhakarbebd8d12014-05-16 10:33:35 -0300221
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300222 common->set_addr(addr + common->ytop_off,
223 addr + common->ybtm_off,
224 addr + common->ctop_off,
225 addr + common->cbtm_off);
226
227 /**
228 * Set interrupt for both the fields in VPIF Register enable channel in
229 * VPIF register
230 */
Lad, Prabhakar9e184042012-09-14 10:22:24 -0300231 channel_first_int[VPIF_VIDEO_INDEX][ch->channel_id] = 1;
Lad, Prabhakar41b9f242014-05-16 10:33:39 -0300232 if (VPIF_CHANNEL0_VIDEO == ch->channel_id) {
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300233 channel0_intr_assert();
234 channel0_intr_enable(1);
235 enable_channel0(1);
236 }
Lad, Prabhakar41b9f242014-05-16 10:33:39 -0300237 if (VPIF_CHANNEL1_VIDEO == ch->channel_id ||
Lad, Prabhakarc66238f2014-05-16 10:33:45 -0300238 ycmux_mode == 2) {
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300239 channel1_intr_assert();
240 channel1_intr_enable(1);
241 enable_channel1(1);
242 }
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300243
244 return 0;
Lad, Prabhakarbebd8d12014-05-16 10:33:35 -0300245
246err:
Kevin Hilman62dd4ac2016-12-07 16:30:23 -0200247 spin_lock_irqsave(&common->irqlock, flags);
Lad, Prabhakarbebd8d12014-05-16 10:33:35 -0300248 list_for_each_entry_safe(buf, tmp, &common->dma_queue, list) {
249 list_del(&buf->list);
Junghak Sung2d700712015-09-22 10:30:30 -0300250 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_QUEUED);
Lad, Prabhakarbebd8d12014-05-16 10:33:35 -0300251 }
Dan Carpenter92491962014-06-12 04:01:45 -0300252 spin_unlock_irqrestore(&common->irqlock, flags);
Lad, Prabhakarbebd8d12014-05-16 10:33:35 -0300253
254 return ret;
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300255}
256
Lad, Prabhakar41b9f242014-05-16 10:33:39 -0300257/**
258 * vpif_stop_streaming : Stop the DMA engine
259 * @vq: ptr to vb2_queue
260 *
261 * This callback stops the DMA engine and any remaining buffers
262 * in the DMA queue are released.
263 */
Hans Verkuile37559b2014-04-17 02:47:21 -0300264static void vpif_stop_streaming(struct vb2_queue *vq)
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300265{
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -0300266 struct channel_obj *ch = vb2_get_drv_priv(vq);
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300267 struct common_obj *common;
Hans Verkuilaec96832012-11-16 12:03:06 -0300268 unsigned long flags;
Lad, Prabhakard9a3b132014-05-16 10:33:42 -0300269 int ret;
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300270
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300271 common = &ch->common[VPIF_VIDEO_INDEX];
272
Lad, Prabhakare6ba3db2014-03-22 08:03:07 -0300273 /* Disable channel as per its device type and channel id */
274 if (VPIF_CHANNEL0_VIDEO == ch->channel_id) {
275 enable_channel0(0);
276 channel0_intr_enable(0);
277 }
Lad, Prabhakar41b9f242014-05-16 10:33:39 -0300278 if (VPIF_CHANNEL1_VIDEO == ch->channel_id ||
Lad, Prabhakarc66238f2014-05-16 10:33:45 -0300279 ycmux_mode == 2) {
Lad, Prabhakare6ba3db2014-03-22 08:03:07 -0300280 enable_channel1(0);
281 channel1_intr_enable(0);
282 }
Lad, Prabhakarc66238f2014-05-16 10:33:45 -0300283
284 ycmux_mode = 0;
Lad, Prabhakare6ba3db2014-03-22 08:03:07 -0300285
Lad, Prabhakard9a3b132014-05-16 10:33:42 -0300286 ret = v4l2_subdev_call(ch->sd, video, s_stream, 0);
287 if (ret && ret != -ENOIOCTLCMD && ret != -ENODEV)
288 vpif_dbg(1, debug, "stream off failed in subdev\n");
289
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300290 /* release all active buffers */
Lad, Prabhakare6ba3db2014-03-22 08:03:07 -0300291 if (common->cur_frm == common->next_frm) {
Junghak Sung2d700712015-09-22 10:30:30 -0300292 vb2_buffer_done(&common->cur_frm->vb.vb2_buf,
293 VB2_BUF_STATE_ERROR);
Lad, Prabhakare6ba3db2014-03-22 08:03:07 -0300294 } else {
Markus Elfring03161cd2016-10-12 10:20:34 -0300295 if (common->cur_frm)
Junghak Sung2d700712015-09-22 10:30:30 -0300296 vb2_buffer_done(&common->cur_frm->vb.vb2_buf,
Lad, Prabhakare6ba3db2014-03-22 08:03:07 -0300297 VB2_BUF_STATE_ERROR);
Markus Elfring03161cd2016-10-12 10:20:34 -0300298 if (common->next_frm)
Junghak Sung2d700712015-09-22 10:30:30 -0300299 vb2_buffer_done(&common->next_frm->vb.vb2_buf,
Lad, Prabhakare6ba3db2014-03-22 08:03:07 -0300300 VB2_BUF_STATE_ERROR);
301 }
302
Kevin Hilman62dd4ac2016-12-07 16:30:23 -0200303 spin_lock_irqsave(&common->irqlock, flags);
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300304 while (!list_empty(&common->dma_queue)) {
305 common->next_frm = list_entry(common->dma_queue.next,
306 struct vpif_cap_buffer, list);
307 list_del(&common->next_frm->list);
Junghak Sung2d700712015-09-22 10:30:30 -0300308 vb2_buffer_done(&common->next_frm->vb.vb2_buf,
309 VB2_BUF_STATE_ERROR);
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300310 }
Hans Verkuilaec96832012-11-16 12:03:06 -0300311 spin_unlock_irqrestore(&common->irqlock, flags);
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300312}
313
314static struct vb2_ops video_qops = {
315 .queue_setup = vpif_buffer_queue_setup,
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300316 .buf_prepare = vpif_buffer_prepare,
317 .start_streaming = vpif_start_streaming,
318 .stop_streaming = vpif_stop_streaming,
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300319 .buf_queue = vpif_buffer_queue,
Prabhakar Ladb41a97a2014-11-26 19:42:33 -0300320 .wait_prepare = vb2_ops_wait_prepare,
321 .wait_finish = vb2_ops_wait_finish,
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300322};
323
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300324/**
325 * vpif_process_buffer_complete: process a completed buffer
326 * @common: ptr to common channel object
327 *
328 * This function time stamp the buffer and mark it as DONE. It also
329 * wake up any process waiting on the QUEUE and set the next buffer
330 * as current
331 */
332static void vpif_process_buffer_complete(struct common_obj *common)
333{
Junghak Sungd6dd6452015-11-03 08:16:37 -0200334 common->cur_frm->vb.vb2_buf.timestamp = ktime_get_ns();
Junghak Sung2d700712015-09-22 10:30:30 -0300335 vb2_buffer_done(&common->cur_frm->vb.vb2_buf, VB2_BUF_STATE_DONE);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300336 /* Make curFrm pointing to nextFrm */
337 common->cur_frm = common->next_frm;
338}
339
340/**
341 * vpif_schedule_next_buffer: set next buffer address for capture
342 * @common : ptr to common channel object
343 *
344 * This function will get next buffer from the dma queue and
345 * set the buffer address in the vpif register for capture.
346 * the buffer is marked active
347 */
348static void vpif_schedule_next_buffer(struct common_obj *common)
349{
350 unsigned long addr = 0;
351
Hans Verkuilaec96832012-11-16 12:03:06 -0300352 spin_lock(&common->irqlock);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300353 common->next_frm = list_entry(common->dma_queue.next,
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300354 struct vpif_cap_buffer, list);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300355 /* Remove that buffer from the buffer queue */
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300356 list_del(&common->next_frm->list);
Hans Verkuilaec96832012-11-16 12:03:06 -0300357 spin_unlock(&common->irqlock);
Junghak Sung2d700712015-09-22 10:30:30 -0300358 addr = vb2_dma_contig_plane_dma_addr(&common->next_frm->vb.vb2_buf, 0);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300359
360 /* Set top and bottom field addresses in VPIF registers */
361 common->set_addr(addr + common->ytop_off,
362 addr + common->ybtm_off,
363 addr + common->ctop_off,
364 addr + common->cbtm_off);
365}
366
367/**
368 * vpif_channel_isr : ISR handler for vpif capture
369 * @irq: irq number
370 * @dev_id: dev_id ptr
371 *
372 * It changes status of the captured buffer, takes next buffer from the queue
Mats Randgaard2c0ddd12010-12-16 12:17:45 -0300373 * and sets its address in VPIF registers
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300374 */
375static irqreturn_t vpif_channel_isr(int irq, void *dev_id)
376{
377 struct vpif_device *dev = &vpif_obj;
378 struct common_obj *common;
379 struct channel_obj *ch;
Markus Elfringc64d61d2016-10-12 10:25:08 -0300380 int channel_id;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300381 int fid = -1, i;
382
383 channel_id = *(int *)(dev_id);
Manjunath Hadlib1fc4232012-04-13 04:43:10 -0300384 if (!vpif_intr_status(channel_id))
385 return IRQ_NONE;
386
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300387 ch = dev->dev[channel_id];
388
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300389 for (i = 0; i < VPIF_NUMBER_OF_OBJECTS; i++) {
390 common = &ch->common[i];
391 /* skip If streaming is not started in this channel */
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300392 /* Check the field format */
Kevin Hilmandde32d42017-06-06 20:37:40 -0300393 if (1 == ch->vpifparams.std_info.frm_fmt ||
394 common->fmt.fmt.pix.field == V4L2_FIELD_NONE) {
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300395 /* Progressive mode */
Hans Verkuilaec96832012-11-16 12:03:06 -0300396 spin_lock(&common->irqlock);
397 if (list_empty(&common->dma_queue)) {
398 spin_unlock(&common->irqlock);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300399 continue;
Hans Verkuilaec96832012-11-16 12:03:06 -0300400 }
401 spin_unlock(&common->irqlock);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300402
403 if (!channel_first_int[i][channel_id])
404 vpif_process_buffer_complete(common);
405
406 channel_first_int[i][channel_id] = 0;
407
408 vpif_schedule_next_buffer(common);
409
410
411 channel_first_int[i][channel_id] = 0;
412 } else {
413 /**
414 * Interlaced mode. If it is first interrupt, ignore
415 * it
416 */
417 if (channel_first_int[i][channel_id]) {
418 channel_first_int[i][channel_id] = 0;
419 continue;
420 }
421 if (0 == i) {
422 ch->field_id ^= 1;
423 /* Get field id from VPIF registers */
424 fid = vpif_channel_getfid(ch->channel_id);
425 if (fid != ch->field_id) {
426 /**
427 * If field id does not match stored
428 * field id, make them in sync
429 */
430 if (0 == fid)
431 ch->field_id = fid;
432 return IRQ_HANDLED;
433 }
434 }
435 /* device field id and local field id are in sync */
436 if (0 == fid) {
437 /* this is even field */
438 if (common->cur_frm == common->next_frm)
439 continue;
440
441 /* mark the current buffer as done */
442 vpif_process_buffer_complete(common);
443 } else if (1 == fid) {
444 /* odd field */
Hans Verkuilaec96832012-11-16 12:03:06 -0300445 spin_lock(&common->irqlock);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300446 if (list_empty(&common->dma_queue) ||
Hans Verkuilaec96832012-11-16 12:03:06 -0300447 (common->cur_frm != common->next_frm)) {
448 spin_unlock(&common->irqlock);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300449 continue;
Hans Verkuilaec96832012-11-16 12:03:06 -0300450 }
451 spin_unlock(&common->irqlock);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300452
453 vpif_schedule_next_buffer(common);
454 }
455 }
456 }
457 return IRQ_HANDLED;
458}
459
460/**
461 * vpif_update_std_info() - update standard related info
462 * @ch: ptr to channel object
463 *
464 * For a given standard selected by application, update values
465 * in the device data structures
466 */
467static int vpif_update_std_info(struct channel_obj *ch)
468{
469 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
470 struct vpif_params *vpifparams = &ch->vpifparams;
471 const struct vpif_channel_config_params *config;
Mats Randgaard2c0ddd12010-12-16 12:17:45 -0300472 struct vpif_channel_config_params *std_info = &vpifparams->std_info;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300473 struct video_obj *vid_ch = &ch->video;
474 int index;
Kevin Hilmandde32d42017-06-06 20:37:40 -0300475 struct v4l2_pix_format *pixfmt = &common->fmt.fmt.pix;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300476
477 vpif_dbg(2, debug, "vpif_update_std_info\n");
478
Kevin Hilmandde32d42017-06-06 20:37:40 -0300479 /*
480 * if called after try_fmt or g_fmt, there will already be a size
481 * so use that by default.
482 */
483 if (pixfmt->width && pixfmt->height) {
484 if (pixfmt->field == V4L2_FIELD_ANY ||
485 pixfmt->field == V4L2_FIELD_NONE)
486 pixfmt->field = V4L2_FIELD_NONE;
487
488 vpifparams->iface.if_type = VPIF_IF_BT656;
489 if (pixfmt->pixelformat == V4L2_PIX_FMT_SGRBG10 ||
490 pixfmt->pixelformat == V4L2_PIX_FMT_SBGGR8)
491 vpifparams->iface.if_type = VPIF_IF_RAW_BAYER;
492
493 if (pixfmt->pixelformat == V4L2_PIX_FMT_SGRBG10)
494 vpifparams->params.data_sz = 1; /* 10 bits/pixel. */
495
496 /*
497 * For raw formats from camera sensors, we don't need
498 * the std_info from table lookup, so nothing else to do here.
499 */
500 if (vpifparams->iface.if_type == VPIF_IF_RAW_BAYER) {
501 memset(std_info, 0, sizeof(struct vpif_channel_config_params));
502 vpifparams->std_info.capture_format = 1; /* CCD/raw mode */
503 return 0;
504 }
505 }
506
Mats Randgaardaa444402010-12-16 12:17:42 -0300507 for (index = 0; index < vpif_ch_params_count; index++) {
Lad, Prabhakarced9b212013-03-20 01:28:27 -0300508 config = &vpif_ch_params[index];
Mats Randgaard40c8bce2010-12-16 12:17:43 -0300509 if (config->hd_sd == 0) {
510 vpif_dbg(2, debug, "SD format\n");
511 if (config->stdid & vid_ch->stdid) {
512 memcpy(std_info, config, sizeof(*config));
513 break;
514 }
515 } else {
516 vpif_dbg(2, debug, "HD format\n");
Hans Verkuil0598c172012-09-18 07:18:47 -0300517 if (!memcmp(&config->dv_timings, &vid_ch->dv_timings,
518 sizeof(vid_ch->dv_timings))) {
Mats Randgaard40c8bce2010-12-16 12:17:43 -0300519 memcpy(std_info, config, sizeof(*config));
520 break;
521 }
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300522 }
523 }
524
525 /* standard not found */
Mats Randgaardaa444402010-12-16 12:17:42 -0300526 if (index == vpif_ch_params_count)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300527 return -EINVAL;
528
529 common->fmt.fmt.pix.width = std_info->width;
530 common->width = std_info->width;
531 common->fmt.fmt.pix.height = std_info->height;
532 common->height = std_info->height;
Lad, Prabhakara4965c52014-05-16 10:33:54 -0300533 common->fmt.fmt.pix.sizeimage = common->height * common->width * 2;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300534 common->fmt.fmt.pix.bytesperline = std_info->width;
535 vpifparams->video_params.hpitch = std_info->width;
536 vpifparams->video_params.storage_mode = std_info->frm_fmt;
Mats Randgaard2c0ddd12010-12-16 12:17:45 -0300537
Lad, Prabhakara4965c52014-05-16 10:33:54 -0300538 if (vid_ch->stdid)
539 common->fmt.fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
540 else
541 common->fmt.fmt.pix.colorspace = V4L2_COLORSPACE_REC709;
542
543 if (ch->vpifparams.std_info.frm_fmt)
544 common->fmt.fmt.pix.field = V4L2_FIELD_NONE;
545 else
546 common->fmt.fmt.pix.field = V4L2_FIELD_INTERLACED;
547
548 if (ch->vpifparams.iface.if_type == VPIF_IF_RAW_BAYER)
549 common->fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_SBGGR8;
550 else
Nori, Sekhar8946ab32017-05-26 07:55:27 -0300551 common->fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_NV16;
Lad, Prabhakara4965c52014-05-16 10:33:54 -0300552
553 common->fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
554
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300555 return 0;
556}
557
558/**
559 * vpif_calculate_offsets : This function calculates buffers offsets
560 * @ch : ptr to channel object
561 *
562 * This function calculates buffer offsets for Y and C in the top and
563 * bottom field
564 */
565static void vpif_calculate_offsets(struct channel_obj *ch)
566{
Mauro Carvalho Chehab24ab6332014-08-21 15:49:06 -0500567 unsigned int hpitch, sizeimage;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300568 struct video_obj *vid_ch = &(ch->video);
569 struct vpif_params *vpifparams = &ch->vpifparams;
570 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
571 enum v4l2_field field = common->fmt.fmt.pix.field;
572
573 vpif_dbg(2, debug, "vpif_calculate_offsets\n");
574
575 if (V4L2_FIELD_ANY == field) {
576 if (vpifparams->std_info.frm_fmt)
577 vid_ch->buf_field = V4L2_FIELD_NONE;
578 else
579 vid_ch->buf_field = V4L2_FIELD_INTERLACED;
580 } else
581 vid_ch->buf_field = common->fmt.fmt.pix.field;
582
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300583 sizeimage = common->fmt.fmt.pix.sizeimage;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300584
585 hpitch = common->fmt.fmt.pix.bytesperline;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300586
587 if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
588 (V4L2_FIELD_INTERLACED == vid_ch->buf_field)) {
589 /* Calculate offsets for Y top, Y Bottom, C top and C Bottom */
590 common->ytop_off = 0;
591 common->ybtm_off = hpitch;
592 common->ctop_off = sizeimage / 2;
593 common->cbtm_off = sizeimage / 2 + hpitch;
594 } else if (V4L2_FIELD_SEQ_TB == vid_ch->buf_field) {
595 /* Calculate offsets for Y top, Y Bottom, C top and C Bottom */
596 common->ytop_off = 0;
597 common->ybtm_off = sizeimage / 4;
598 common->ctop_off = sizeimage / 2;
599 common->cbtm_off = common->ctop_off + sizeimage / 4;
600 } else if (V4L2_FIELD_SEQ_BT == vid_ch->buf_field) {
601 /* Calculate offsets for Y top, Y Bottom, C top and C Bottom */
602 common->ybtm_off = 0;
603 common->ytop_off = sizeimage / 4;
604 common->cbtm_off = sizeimage / 2;
605 common->ctop_off = common->cbtm_off + sizeimage / 4;
606 }
607 if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
608 (V4L2_FIELD_INTERLACED == vid_ch->buf_field))
609 vpifparams->video_params.storage_mode = 1;
610 else
611 vpifparams->video_params.storage_mode = 0;
612
613 if (1 == vpifparams->std_info.frm_fmt)
614 vpifparams->video_params.hpitch =
615 common->fmt.fmt.pix.bytesperline;
616 else {
617 if ((field == V4L2_FIELD_ANY)
618 || (field == V4L2_FIELD_INTERLACED))
619 vpifparams->video_params.hpitch =
620 common->fmt.fmt.pix.bytesperline * 2;
621 else
622 vpifparams->video_params.hpitch =
623 common->fmt.fmt.pix.bytesperline;
624 }
625
626 ch->vpifparams.video_params.stdid = vpifparams->std_info.stdid;
627}
628
629/**
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300630 * vpif_get_default_field() - Get default field type based on interface
631 * @vpif_params - ptr to vpif params
632 */
633static inline enum v4l2_field vpif_get_default_field(
634 struct vpif_interface *iface)
635{
636 return (iface->if_type == VPIF_IF_RAW_BAYER) ? V4L2_FIELD_NONE :
637 V4L2_FIELD_INTERLACED;
638}
639
640/**
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300641 * vpif_config_addr() - function to configure buffer address in vpif
642 * @ch - channel ptr
643 * @muxmode - channel mux mode
644 */
645static void vpif_config_addr(struct channel_obj *ch, int muxmode)
646{
647 struct common_obj *common;
648
649 vpif_dbg(2, debug, "vpif_config_addr\n");
650
651 common = &(ch->common[VPIF_VIDEO_INDEX]);
652
653 if (VPIF_CHANNEL1_VIDEO == ch->channel_id)
654 common->set_addr = ch1_set_videobuf_addr;
655 else if (2 == muxmode)
656 common->set_addr = ch0_set_videobuf_addr_yc_nmux;
657 else
658 common->set_addr = ch0_set_videobuf_addr;
659}
660
661/**
Hans Verkuil178cce12012-09-20 09:06:30 -0300662 * vpif_input_to_subdev() - Maps input to sub device
663 * @vpif_cfg - global config ptr
664 * @chan_cfg - channel config ptr
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300665 * @input_index - Given input index from application
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300666 *
667 * lookup the sub device information for a given input index.
668 * we report all the inputs to application. inputs table also
669 * has sub device name for the each input
670 */
Hans Verkuil178cce12012-09-20 09:06:30 -0300671static int vpif_input_to_subdev(
672 struct vpif_capture_config *vpif_cfg,
673 struct vpif_capture_chan_config *chan_cfg,
674 int input_index)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300675{
Hans Verkuil178cce12012-09-20 09:06:30 -0300676 struct vpif_subdev_info *subdev_info;
677 const char *subdev_name;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300678 int i;
679
Hans Verkuil178cce12012-09-20 09:06:30 -0300680 vpif_dbg(2, debug, "vpif_input_to_subdev\n");
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300681
Kevin Hilmanbff782d2016-12-16 22:47:54 -0200682 if (!chan_cfg)
683 return -1;
684 if (input_index >= chan_cfg->input_count)
685 return -1;
Hans Verkuil178cce12012-09-20 09:06:30 -0300686 subdev_name = chan_cfg->inputs[input_index].subdev_name;
Markus Elfring03161cd2016-10-12 10:20:34 -0300687 if (!subdev_name)
Hans Verkuil178cce12012-09-20 09:06:30 -0300688 return -1;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300689
690 /* loop through the sub device list to get the sub device info */
691 for (i = 0; i < vpif_cfg->subdev_count; i++) {
692 subdev_info = &vpif_cfg->subdev_info[i];
Kevin Hilman4a5f8ae2017-06-06 20:37:39 -0300693 if (subdev_info && !strcmp(subdev_info->name, subdev_name))
Hans Verkuil178cce12012-09-20 09:06:30 -0300694 return i;
695 }
696 return -1;
697}
698
699/**
700 * vpif_set_input() - Select an input
701 * @vpif_cfg - global config ptr
702 * @ch - channel
703 * @_index - Given input index from application
704 *
705 * Select the given input.
706 */
707static int vpif_set_input(
708 struct vpif_capture_config *vpif_cfg,
709 struct channel_obj *ch,
710 int index)
711{
712 struct vpif_capture_chan_config *chan_cfg =
713 &vpif_cfg->chan_config[ch->channel_id];
714 struct vpif_subdev_info *subdev_info = NULL;
715 struct v4l2_subdev *sd = NULL;
716 u32 input = 0, output = 0;
717 int sd_index;
718 int ret;
719
720 sd_index = vpif_input_to_subdev(vpif_cfg, chan_cfg, index);
721 if (sd_index >= 0) {
722 sd = vpif_obj.sd[sd_index];
723 subdev_info = &vpif_cfg->subdev_info[sd_index];
Kevin Hilmanbff782d2016-12-16 22:47:54 -0200724 } else {
725 /* no subdevice, no input to setup */
726 return 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300727 }
728
Hans Verkuil178cce12012-09-20 09:06:30 -0300729 /* first setup input path from sub device to vpif */
730 if (sd && vpif_cfg->setup_input_path) {
731 ret = vpif_cfg->setup_input_path(ch->channel_id,
732 subdev_info->name);
733 if (ret < 0) {
734 vpif_dbg(1, debug, "couldn't setup input path for the" \
735 " sub device %s, for input index %d\n",
736 subdev_info->name, index);
737 return ret;
738 }
739 }
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300740
Hans Verkuil178cce12012-09-20 09:06:30 -0300741 if (sd) {
742 input = chan_cfg->inputs[index].input_route;
743 output = chan_cfg->inputs[index].output_route;
744 ret = v4l2_subdev_call(sd, video, s_routing,
745 input, output, 0);
746 if (ret < 0 && ret != -ENOIOCTLCMD) {
747 vpif_dbg(1, debug, "Failed to set input\n");
748 return ret;
749 }
750 }
751 ch->input_idx = index;
752 ch->sd = sd;
753 /* copy interface parameters to vpif */
Hans Verkuil0d4f35f2012-09-20 09:06:32 -0300754 ch->vpifparams.iface = chan_cfg->vpif_if;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300755
Hans Verkuil178cce12012-09-20 09:06:30 -0300756 /* update tvnorms from the sub device input info */
Lad, Prabhakar8eef6662015-03-08 18:57:23 -0300757 ch->video_dev.tvnorms = chan_cfg->inputs[index].input.std;
Hans Verkuil178cce12012-09-20 09:06:30 -0300758 return 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300759}
760
761/**
762 * vpif_querystd() - querystd handler
763 * @file: file ptr
764 * @priv: file handle
765 * @std_id: ptr to std id
766 *
767 * This function is called to detect standard at the selected input
768 */
769static int vpif_querystd(struct file *file, void *priv, v4l2_std_id *std_id)
770{
Lad, Prabhakar7ff51192014-05-16 10:33:41 -0300771 struct video_device *vdev = video_devdata(file);
772 struct channel_obj *ch = video_get_drvdata(vdev);
Markus Elfring6a842cc2016-10-12 10:22:45 -0300773 int ret;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300774
775 vpif_dbg(2, debug, "vpif_querystd\n");
776
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300777 /* Call querystd function of decoder device */
Hans Verkuil178cce12012-09-20 09:06:30 -0300778 ret = v4l2_subdev_call(ch->sd, video, querystd, std_id);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300779
Hans Verkuil178cce12012-09-20 09:06:30 -0300780 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
781 return -ENODATA;
782 if (ret) {
783 vpif_dbg(1, debug, "Failed to query standard for sub devices\n");
784 return ret;
785 }
786
787 return 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300788}
789
790/**
791 * vpif_g_std() - get STD handler
792 * @file: file ptr
793 * @priv: file handle
794 * @std_id: ptr to std id
795 */
796static int vpif_g_std(struct file *file, void *priv, v4l2_std_id *std)
797{
Lad, Prabhakard557b7d2014-05-16 10:33:52 -0300798 struct vpif_capture_config *config = vpif_dev->platform_data;
Lad, Prabhakar7ff51192014-05-16 10:33:41 -0300799 struct video_device *vdev = video_devdata(file);
800 struct channel_obj *ch = video_get_drvdata(vdev);
Lad, Prabhakard557b7d2014-05-16 10:33:52 -0300801 struct vpif_capture_chan_config *chan_cfg;
802 struct v4l2_input input;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300803
804 vpif_dbg(2, debug, "vpif_g_std\n");
805
Markus Elfring03161cd2016-10-12 10:20:34 -0300806 if (!config->chan_config[ch->channel_id].inputs)
Lad, Prabhakard557b7d2014-05-16 10:33:52 -0300807 return -ENODATA;
808
809 chan_cfg = &config->chan_config[ch->channel_id];
810 input = chan_cfg->inputs[ch->input_idx].input;
811 if (input.capabilities != V4L2_IN_CAP_STD)
812 return -ENODATA;
813
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300814 *std = ch->video.stdid;
815 return 0;
816}
817
818/**
819 * vpif_s_std() - set STD handler
820 * @file: file ptr
821 * @priv: file handle
822 * @std_id: ptr to std id
823 */
Hans Verkuil314527a2013-03-15 06:10:40 -0300824static int vpif_s_std(struct file *file, void *priv, v4l2_std_id std_id)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300825{
Lad, Prabhakard557b7d2014-05-16 10:33:52 -0300826 struct vpif_capture_config *config = vpif_dev->platform_data;
Lad, Prabhakar7ff51192014-05-16 10:33:41 -0300827 struct video_device *vdev = video_devdata(file);
828 struct channel_obj *ch = video_get_drvdata(vdev);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300829 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
Lad, Prabhakard557b7d2014-05-16 10:33:52 -0300830 struct vpif_capture_chan_config *chan_cfg;
831 struct v4l2_input input;
832 int ret;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300833
834 vpif_dbg(2, debug, "vpif_s_std\n");
835
Markus Elfring03161cd2016-10-12 10:20:34 -0300836 if (!config->chan_config[ch->channel_id].inputs)
Lad, Prabhakard557b7d2014-05-16 10:33:52 -0300837 return -ENODATA;
838
839 chan_cfg = &config->chan_config[ch->channel_id];
840 input = chan_cfg->inputs[ch->input_idx].input;
841 if (input.capabilities != V4L2_IN_CAP_STD)
842 return -ENODATA;
843
Lad, Prabhakarc66238f2014-05-16 10:33:45 -0300844 if (vb2_is_busy(&common->buffer_queue))
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300845 return -EBUSY;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300846
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300847 /* Call encoder subdevice function to set the standard */
Hans Verkuil314527a2013-03-15 06:10:40 -0300848 ch->video.stdid = std_id;
Hans Verkuil0598c172012-09-18 07:18:47 -0300849 memset(&ch->video.dv_timings, 0, sizeof(ch->video.dv_timings));
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300850
851 /* Get the information about the standard */
852 if (vpif_update_std_info(ch)) {
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300853 vpif_err("Error getting the standard info\n");
Hans Verkuil46656af2011-01-04 06:51:35 -0300854 return -EINVAL;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300855 }
856
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300857 /* set standard in the sub device */
Laurent Pinchart8774bed2014-04-28 16:53:01 -0300858 ret = v4l2_subdev_call(ch->sd, video, s_std, std_id);
Hans Verkuil178cce12012-09-20 09:06:30 -0300859 if (ret && ret != -ENOIOCTLCMD && ret != -ENODEV) {
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300860 vpif_dbg(1, debug, "Failed to set standard for sub devices\n");
Hans Verkuil178cce12012-09-20 09:06:30 -0300861 return ret;
862 }
863 return 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300864}
865
866/**
867 * vpif_enum_input() - ENUMINPUT handler
868 * @file: file ptr
869 * @priv: file handle
870 * @input: ptr to input structure
871 */
872static int vpif_enum_input(struct file *file, void *priv,
873 struct v4l2_input *input)
874{
875
876 struct vpif_capture_config *config = vpif_dev->platform_data;
Lad, Prabhakar7ff51192014-05-16 10:33:41 -0300877 struct video_device *vdev = video_devdata(file);
878 struct channel_obj *ch = video_get_drvdata(vdev);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300879 struct vpif_capture_chan_config *chan_cfg;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300880
881 chan_cfg = &config->chan_config[ch->channel_id];
882
Lad, Prabhakara4965c52014-05-16 10:33:54 -0300883 if (input->index >= chan_cfg->input_count)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300884 return -EINVAL;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300885
886 memcpy(input, &chan_cfg->inputs[input->index].input,
887 sizeof(*input));
888 return 0;
889}
890
891/**
892 * vpif_g_input() - Get INPUT handler
893 * @file: file ptr
894 * @priv: file handle
895 * @index: ptr to input index
896 */
897static int vpif_g_input(struct file *file, void *priv, unsigned int *index)
898{
Lad, Prabhakar7ff51192014-05-16 10:33:41 -0300899 struct video_device *vdev = video_devdata(file);
900 struct channel_obj *ch = video_get_drvdata(vdev);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300901
Hans Verkuil6f47c6c2012-09-20 09:06:22 -0300902 *index = ch->input_idx;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300903 return 0;
904}
905
906/**
907 * vpif_s_input() - Set INPUT handler
908 * @file: file ptr
909 * @priv: file handle
910 * @index: input index
911 */
912static int vpif_s_input(struct file *file, void *priv, unsigned int index)
913{
914 struct vpif_capture_config *config = vpif_dev->platform_data;
Lad, Prabhakar7ff51192014-05-16 10:33:41 -0300915 struct video_device *vdev = video_devdata(file);
916 struct channel_obj *ch = video_get_drvdata(vdev);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300917 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
Lad, Prabhakar7ff51192014-05-16 10:33:41 -0300918 struct vpif_capture_chan_config *chan_cfg;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300919
920 chan_cfg = &config->chan_config[ch->channel_id];
921
Hans Verkuil7aaad132012-09-20 09:06:25 -0300922 if (index >= chan_cfg->input_count)
923 return -EINVAL;
924
Lad, Prabhakarc66238f2014-05-16 10:33:45 -0300925 if (vb2_is_busy(&common->buffer_queue))
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300926 return -EBUSY;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300927
Hans Verkuil178cce12012-09-20 09:06:30 -0300928 return vpif_set_input(config, ch, index);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300929}
930
931/**
932 * vpif_enum_fmt_vid_cap() - ENUM_FMT handler
933 * @file: file ptr
934 * @priv: file handle
935 * @index: input index
936 */
937static int vpif_enum_fmt_vid_cap(struct file *file, void *priv,
938 struct v4l2_fmtdesc *fmt)
939{
Lad, Prabhakar7ff51192014-05-16 10:33:41 -0300940 struct video_device *vdev = video_devdata(file);
941 struct channel_obj *ch = video_get_drvdata(vdev);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300942
943 if (fmt->index != 0) {
944 vpif_dbg(1, debug, "Invalid format index\n");
945 return -EINVAL;
946 }
947
948 /* Fill in the information about format */
949 if (ch->vpifparams.iface.if_type == VPIF_IF_RAW_BAYER) {
950 fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
951 strcpy(fmt->description, "Raw Mode -Bayer Pattern GrRBGb");
952 fmt->pixelformat = V4L2_PIX_FMT_SBGGR8;
953 } else {
954 fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Nori, Sekhar8946ab32017-05-26 07:55:27 -0300955 strcpy(fmt->description, "YCbCr4:2:2 Semi-Planar");
956 fmt->pixelformat = V4L2_PIX_FMT_NV16;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300957 }
958 return 0;
959}
960
961/**
962 * vpif_try_fmt_vid_cap() - TRY_FMT handler
963 * @file: file ptr
964 * @priv: file handle
965 * @fmt: ptr to v4l2 format structure
966 */
967static int vpif_try_fmt_vid_cap(struct file *file, void *priv,
968 struct v4l2_format *fmt)
969{
Lad, Prabhakar7ff51192014-05-16 10:33:41 -0300970 struct video_device *vdev = video_devdata(file);
971 struct channel_obj *ch = video_get_drvdata(vdev);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300972 struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
Lad, Prabhakara4965c52014-05-16 10:33:54 -0300973 struct common_obj *common = &(ch->common[VPIF_VIDEO_INDEX]);
Lad, Prabhakara4965c52014-05-16 10:33:54 -0300974
Kevin Hilmandde32d42017-06-06 20:37:40 -0300975 common->fmt = *fmt;
Lad, Prabhakara4965c52014-05-16 10:33:54 -0300976 vpif_update_std_info(ch);
977
978 pixfmt->field = common->fmt.fmt.pix.field;
979 pixfmt->colorspace = common->fmt.fmt.pix.colorspace;
980 pixfmt->bytesperline = common->fmt.fmt.pix.width;
981 pixfmt->width = common->fmt.fmt.pix.width;
982 pixfmt->height = common->fmt.fmt.pix.height;
983 pixfmt->sizeimage = pixfmt->bytesperline * pixfmt->height * 2;
Kevin Hilmandde32d42017-06-06 20:37:40 -0300984 if (pixfmt->pixelformat == V4L2_PIX_FMT_SGRBG10) {
985 pixfmt->bytesperline = common->fmt.fmt.pix.width * 2;
986 pixfmt->sizeimage = pixfmt->bytesperline * pixfmt->height;
987 }
Lad, Prabhakara4965c52014-05-16 10:33:54 -0300988 pixfmt->priv = 0;
989
Kevin Hilmandde32d42017-06-06 20:37:40 -0300990 dev_dbg(vpif_dev, "%s: %d x %d; pitch=%d pixelformat=0x%08x, field=%d, size=%d\n", __func__,
991 pixfmt->width, pixfmt->height,
992 pixfmt->bytesperline, pixfmt->pixelformat,
993 pixfmt->field, pixfmt->sizeimage);
994
Lad, Prabhakara4965c52014-05-16 10:33:54 -0300995 return 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300996}
997
998
999/**
1000 * vpif_g_fmt_vid_cap() - Set INPUT handler
1001 * @file: file ptr
1002 * @priv: file handle
1003 * @fmt: ptr to v4l2 format structure
1004 */
1005static int vpif_g_fmt_vid_cap(struct file *file, void *priv,
1006 struct v4l2_format *fmt)
1007{
Lad, Prabhakar7ff51192014-05-16 10:33:41 -03001008 struct video_device *vdev = video_devdata(file);
1009 struct channel_obj *ch = video_get_drvdata(vdev);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001010 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
Kevin Hilmandde32d42017-06-06 20:37:40 -03001011 struct v4l2_pix_format *pix_fmt = &fmt->fmt.pix;
1012 struct v4l2_subdev_format format = {
1013 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
1014 };
1015 struct v4l2_mbus_framefmt *mbus_fmt = &format.format;
1016 int ret;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001017
1018 /* Check the validity of the buffer type */
1019 if (common->fmt.type != fmt->type)
1020 return -EINVAL;
1021
Kevin Hilmandde32d42017-06-06 20:37:40 -03001022 /* By default, use currently set fmt */
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001023 *fmt = common->fmt;
Kevin Hilmandde32d42017-06-06 20:37:40 -03001024
1025 /* If subdev has get_fmt, use that to override */
1026 ret = v4l2_subdev_call(ch->sd, pad, get_fmt, NULL, &format);
1027 if (!ret && mbus_fmt->code) {
1028 v4l2_fill_pix_format(pix_fmt, mbus_fmt);
1029 pix_fmt->bytesperline = pix_fmt->width;
1030 if (mbus_fmt->code == MEDIA_BUS_FMT_SGRBG10_1X10) {
1031 /* e.g. mt9v032 */
1032 pix_fmt->pixelformat = V4L2_PIX_FMT_SGRBG10;
1033 pix_fmt->bytesperline = pix_fmt->width * 2;
1034 } else if (mbus_fmt->code == MEDIA_BUS_FMT_UYVY8_2X8) {
1035 /* e.g. tvp514x */
1036 pix_fmt->pixelformat = V4L2_PIX_FMT_NV16;
1037 pix_fmt->bytesperline = pix_fmt->width * 2;
1038 } else {
1039 dev_warn(vpif_dev, "%s: Unhandled media-bus format 0x%x\n",
1040 __func__, mbus_fmt->code);
1041 }
1042 pix_fmt->sizeimage = pix_fmt->bytesperline * pix_fmt->height;
1043 dev_dbg(vpif_dev, "%s: %d x %d; pitch=%d, pixelformat=0x%08x, code=0x%x, field=%d, size=%d\n", __func__,
1044 pix_fmt->width, pix_fmt->height,
1045 pix_fmt->bytesperline, pix_fmt->pixelformat,
1046 mbus_fmt->code, pix_fmt->field, pix_fmt->sizeimage);
1047
1048 common->fmt = *fmt;
1049 vpif_update_std_info(ch);
1050 }
1051
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001052 return 0;
1053}
1054
1055/**
1056 * vpif_s_fmt_vid_cap() - Set FMT handler
1057 * @file: file ptr
1058 * @priv: file handle
1059 * @fmt: ptr to v4l2 format structure
1060 */
1061static int vpif_s_fmt_vid_cap(struct file *file, void *priv,
1062 struct v4l2_format *fmt)
1063{
Lad, Prabhakar7ff51192014-05-16 10:33:41 -03001064 struct video_device *vdev = video_devdata(file);
1065 struct channel_obj *ch = video_get_drvdata(vdev);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001066 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
Lad, Prabhakara4965c52014-05-16 10:33:54 -03001067 int ret;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001068
Mats Randgaard2c0ddd12010-12-16 12:17:45 -03001069 vpif_dbg(2, debug, "%s\n", __func__);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001070
Lad, Prabhakarc66238f2014-05-16 10:33:45 -03001071 if (vb2_is_busy(&common->buffer_queue))
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001072 return -EBUSY;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001073
Lad, Prabhakara4965c52014-05-16 10:33:54 -03001074 ret = vpif_try_fmt_vid_cap(file, priv, fmt);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001075 if (ret)
1076 return ret;
Lad, Prabhakara4965c52014-05-16 10:33:54 -03001077
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001078 /* store the format in the channel object */
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001079 common->fmt = *fmt;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001080 return 0;
1081}
1082
1083/**
1084 * vpif_querycap() - QUERYCAP handler
1085 * @file: file ptr
1086 * @priv: file handle
1087 * @cap: ptr to v4l2_capability structure
1088 */
1089static int vpif_querycap(struct file *file, void *priv,
1090 struct v4l2_capability *cap)
1091{
1092 struct vpif_capture_config *config = vpif_dev->platform_data;
1093
Lad, Prabhakar626d533f2012-09-25 11:21:55 -03001094 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
1095 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
Lad, Prabhakare75ea0c2014-05-16 10:33:46 -03001096 strlcpy(cap->driver, VPIF_DRIVER_NAME, sizeof(cap->driver));
Lad, Prabhakar626d533f2012-09-25 11:21:55 -03001097 snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
1098 dev_name(vpif_dev));
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001099 strlcpy(cap->card, config->card_name, sizeof(cap->card));
1100
1101 return 0;
1102}
1103
1104/**
Hans Verkuil0598c172012-09-18 07:18:47 -03001105 * vpif_enum_dv_timings() - ENUM_DV_TIMINGS handler
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001106 * @file: file ptr
1107 * @priv: file handle
Hans Verkuil0598c172012-09-18 07:18:47 -03001108 * @timings: input timings
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001109 */
Hans Verkuil0598c172012-09-18 07:18:47 -03001110static int
1111vpif_enum_dv_timings(struct file *file, void *priv,
1112 struct v4l2_enum_dv_timings *timings)
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001113{
Lad, Prabhakar1e8852af2014-05-16 10:33:51 -03001114 struct vpif_capture_config *config = vpif_dev->platform_data;
Lad, Prabhakar7ff51192014-05-16 10:33:41 -03001115 struct video_device *vdev = video_devdata(file);
1116 struct channel_obj *ch = video_get_drvdata(vdev);
Lad, Prabhakar1e8852af2014-05-16 10:33:51 -03001117 struct vpif_capture_chan_config *chan_cfg;
1118 struct v4l2_input input;
Hans Verkuil178cce12012-09-20 09:06:30 -03001119 int ret;
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001120
Markus Elfring03161cd2016-10-12 10:20:34 -03001121 if (!config->chan_config[ch->channel_id].inputs)
Lad, Prabhakar1e8852af2014-05-16 10:33:51 -03001122 return -ENODATA;
1123
1124 chan_cfg = &config->chan_config[ch->channel_id];
1125 input = chan_cfg->inputs[ch->input_idx].input;
1126 if (input.capabilities != V4L2_IN_CAP_DV_TIMINGS)
1127 return -ENODATA;
1128
Laurent Pinchart10d21462014-01-31 09:04:19 -03001129 timings->pad = 0;
1130
1131 ret = v4l2_subdev_call(ch->sd, pad, enum_dv_timings, timings);
Wei Yongjune070f1b2012-10-30 09:45:00 -03001132 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
Hans Verkuil178cce12012-09-20 09:06:30 -03001133 return -EINVAL;
Lad, Prabhakar1e8852af2014-05-16 10:33:51 -03001134
Hans Verkuil178cce12012-09-20 09:06:30 -03001135 return ret;
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001136}
1137
1138/**
Hans Verkuil0598c172012-09-18 07:18:47 -03001139 * vpif_query_dv_timings() - QUERY_DV_TIMINGS handler
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001140 * @file: file ptr
1141 * @priv: file handle
Hans Verkuil0598c172012-09-18 07:18:47 -03001142 * @timings: input timings
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001143 */
Hans Verkuil0598c172012-09-18 07:18:47 -03001144static int
1145vpif_query_dv_timings(struct file *file, void *priv,
1146 struct v4l2_dv_timings *timings)
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001147{
Lad, Prabhakar1e8852af2014-05-16 10:33:51 -03001148 struct vpif_capture_config *config = vpif_dev->platform_data;
Lad, Prabhakar7ff51192014-05-16 10:33:41 -03001149 struct video_device *vdev = video_devdata(file);
1150 struct channel_obj *ch = video_get_drvdata(vdev);
Lad, Prabhakar1e8852af2014-05-16 10:33:51 -03001151 struct vpif_capture_chan_config *chan_cfg;
1152 struct v4l2_input input;
Hans Verkuil178cce12012-09-20 09:06:30 -03001153 int ret;
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001154
Markus Elfring03161cd2016-10-12 10:20:34 -03001155 if (!config->chan_config[ch->channel_id].inputs)
Lad, Prabhakar1e8852af2014-05-16 10:33:51 -03001156 return -ENODATA;
1157
1158 chan_cfg = &config->chan_config[ch->channel_id];
1159 input = chan_cfg->inputs[ch->input_idx].input;
1160 if (input.capabilities != V4L2_IN_CAP_DV_TIMINGS)
1161 return -ENODATA;
1162
Hans Verkuil178cce12012-09-20 09:06:30 -03001163 ret = v4l2_subdev_call(ch->sd, video, query_dv_timings, timings);
Wei Yongjune070f1b2012-10-30 09:45:00 -03001164 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
Hans Verkuil178cce12012-09-20 09:06:30 -03001165 return -ENODATA;
Lad, Prabhakar1e8852af2014-05-16 10:33:51 -03001166
Hans Verkuil178cce12012-09-20 09:06:30 -03001167 return ret;
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001168}
1169
Mats Randgaardc027e162010-12-16 12:17:44 -03001170/**
1171 * vpif_s_dv_timings() - S_DV_TIMINGS handler
1172 * @file: file ptr
1173 * @priv: file handle
1174 * @timings: digital video timings
1175 */
1176static int vpif_s_dv_timings(struct file *file, void *priv,
1177 struct v4l2_dv_timings *timings)
1178{
Lad, Prabhakar1e8852af2014-05-16 10:33:51 -03001179 struct vpif_capture_config *config = vpif_dev->platform_data;
Lad, Prabhakar7ff51192014-05-16 10:33:41 -03001180 struct video_device *vdev = video_devdata(file);
1181 struct channel_obj *ch = video_get_drvdata(vdev);
Mats Randgaardc027e162010-12-16 12:17:44 -03001182 struct vpif_params *vpifparams = &ch->vpifparams;
1183 struct vpif_channel_config_params *std_info = &vpifparams->std_info;
Lad, Prabhakar1e8852af2014-05-16 10:33:51 -03001184 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
Mats Randgaardc027e162010-12-16 12:17:44 -03001185 struct video_obj *vid_ch = &ch->video;
Hans Verkuil0598c172012-09-18 07:18:47 -03001186 struct v4l2_bt_timings *bt = &vid_ch->dv_timings.bt;
Lad, Prabhakar1e8852af2014-05-16 10:33:51 -03001187 struct vpif_capture_chan_config *chan_cfg;
1188 struct v4l2_input input;
Mats Randgaardc027e162010-12-16 12:17:44 -03001189 int ret;
1190
Markus Elfring03161cd2016-10-12 10:20:34 -03001191 if (!config->chan_config[ch->channel_id].inputs)
Lad, Prabhakar1e8852af2014-05-16 10:33:51 -03001192 return -ENODATA;
1193
1194 chan_cfg = &config->chan_config[ch->channel_id];
1195 input = chan_cfg->inputs[ch->input_idx].input;
1196 if (input.capabilities != V4L2_IN_CAP_DV_TIMINGS)
1197 return -ENODATA;
1198
Mats Randgaardc027e162010-12-16 12:17:44 -03001199 if (timings->type != V4L2_DV_BT_656_1120) {
1200 vpif_dbg(2, debug, "Timing type not defined\n");
1201 return -EINVAL;
1202 }
1203
Lad, Prabhakar1e8852af2014-05-16 10:33:51 -03001204 if (vb2_is_busy(&common->buffer_queue))
1205 return -EBUSY;
1206
Mats Randgaardc027e162010-12-16 12:17:44 -03001207 /* Configure subdevice timings, if any */
Hans Verkuil178cce12012-09-20 09:06:30 -03001208 ret = v4l2_subdev_call(ch->sd, video, s_dv_timings, timings);
1209 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
1210 ret = 0;
Mats Randgaardc027e162010-12-16 12:17:44 -03001211 if (ret < 0) {
1212 vpif_dbg(2, debug, "Error setting custom DV timings\n");
1213 return ret;
1214 }
1215
1216 if (!(timings->bt.width && timings->bt.height &&
1217 (timings->bt.hbackporch ||
1218 timings->bt.hfrontporch ||
1219 timings->bt.hsync) &&
1220 timings->bt.vfrontporch &&
1221 (timings->bt.vbackporch ||
1222 timings->bt.vsync))) {
Mauro Carvalho Chehabded026e2016-10-18 17:44:08 -02001223 vpif_dbg(2, debug, "Timings for width, height, horizontal back porch, horizontal sync, horizontal front porch, vertical back porch, vertical sync and vertical back porch must be defined\n");
Mats Randgaardc027e162010-12-16 12:17:44 -03001224 return -EINVAL;
1225 }
1226
Hans Verkuil0598c172012-09-18 07:18:47 -03001227 vid_ch->dv_timings = *timings;
Mats Randgaardc027e162010-12-16 12:17:44 -03001228
1229 /* Configure video port timings */
1230
Hans Verkuile3655262013-07-29 08:41:00 -03001231 std_info->eav2sav = V4L2_DV_BT_BLANKING_WIDTH(bt) - 8;
Mats Randgaardc027e162010-12-16 12:17:44 -03001232 std_info->sav2eav = bt->width;
1233
1234 std_info->l1 = 1;
1235 std_info->l3 = bt->vsync + bt->vbackporch + 1;
1236
Hans Verkuile3655262013-07-29 08:41:00 -03001237 std_info->vsize = V4L2_DV_BT_FRAME_HEIGHT(bt);
Mats Randgaardc027e162010-12-16 12:17:44 -03001238 if (bt->interlaced) {
1239 if (bt->il_vbackporch || bt->il_vfrontporch || bt->il_vsync) {
Mats Randgaardc027e162010-12-16 12:17:44 -03001240 std_info->l5 = std_info->vsize/2 -
1241 (bt->vfrontporch - 1);
1242 std_info->l7 = std_info->vsize/2 + 1;
1243 std_info->l9 = std_info->l7 + bt->il_vsync +
1244 bt->il_vbackporch + 1;
1245 std_info->l11 = std_info->vsize -
1246 (bt->il_vfrontporch - 1);
1247 } else {
Mauro Carvalho Chehabded026e2016-10-18 17:44:08 -02001248 vpif_dbg(2, debug, "Required timing values for interlaced BT format missing\n");
Mats Randgaardc027e162010-12-16 12:17:44 -03001249 return -EINVAL;
1250 }
1251 } else {
Mats Randgaardc027e162010-12-16 12:17:44 -03001252 std_info->l5 = std_info->vsize - (bt->vfrontporch - 1);
1253 }
1254 strncpy(std_info->name, "Custom timings BT656/1120", VPIF_MAX_NAME);
1255 std_info->width = bt->width;
1256 std_info->height = bt->height;
1257 std_info->frm_fmt = bt->interlaced ? 0 : 1;
1258 std_info->ycmux_mode = 0;
1259 std_info->capture_format = 0;
1260 std_info->vbi_supported = 0;
1261 std_info->hd_sd = 1;
1262 std_info->stdid = 0;
Mats Randgaardc027e162010-12-16 12:17:44 -03001263
1264 vid_ch->stdid = 0;
Mats Randgaardc027e162010-12-16 12:17:44 -03001265 return 0;
1266}
1267
1268/**
1269 * vpif_g_dv_timings() - G_DV_TIMINGS handler
1270 * @file: file ptr
1271 * @priv: file handle
1272 * @timings: digital video timings
1273 */
1274static int vpif_g_dv_timings(struct file *file, void *priv,
1275 struct v4l2_dv_timings *timings)
1276{
Lad, Prabhakar1e8852af2014-05-16 10:33:51 -03001277 struct vpif_capture_config *config = vpif_dev->platform_data;
Lad, Prabhakar7ff51192014-05-16 10:33:41 -03001278 struct video_device *vdev = video_devdata(file);
1279 struct channel_obj *ch = video_get_drvdata(vdev);
Mats Randgaardc027e162010-12-16 12:17:44 -03001280 struct video_obj *vid_ch = &ch->video;
Lad, Prabhakar1e8852af2014-05-16 10:33:51 -03001281 struct vpif_capture_chan_config *chan_cfg;
1282 struct v4l2_input input;
1283
Markus Elfring03161cd2016-10-12 10:20:34 -03001284 if (!config->chan_config[ch->channel_id].inputs)
Lad, Prabhakar1e8852af2014-05-16 10:33:51 -03001285 return -ENODATA;
1286
1287 chan_cfg = &config->chan_config[ch->channel_id];
1288 input = chan_cfg->inputs[ch->input_idx].input;
1289 if (input.capabilities != V4L2_IN_CAP_DV_TIMINGS)
1290 return -ENODATA;
Mats Randgaardc027e162010-12-16 12:17:44 -03001291
Hans Verkuil0598c172012-09-18 07:18:47 -03001292 *timings = vid_ch->dv_timings;
Mats Randgaardc027e162010-12-16 12:17:44 -03001293
1294 return 0;
1295}
1296
Mats Randgaard7036d6a2010-12-16 12:17:41 -03001297/*
Mats Randgaard7036d6a2010-12-16 12:17:41 -03001298 * vpif_log_status() - Status information
1299 * @file: file ptr
1300 * @priv: file handle
1301 *
1302 * Returns zero.
1303 */
1304static int vpif_log_status(struct file *filep, void *priv)
1305{
1306 /* status for sub devices */
1307 v4l2_device_call_all(&vpif_obj.v4l2_dev, 0, core, log_status);
1308
1309 return 0;
1310}
1311
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001312/* vpif capture ioctl operations */
1313static const struct v4l2_ioctl_ops vpif_ioctl_ops = {
Lad, Prabhakar7b4657f2014-05-16 10:33:49 -03001314 .vidioc_querycap = vpif_querycap,
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001315 .vidioc_enum_fmt_vid_cap = vpif_enum_fmt_vid_cap,
Lad, Prabhakar7b4657f2014-05-16 10:33:49 -03001316 .vidioc_g_fmt_vid_cap = vpif_g_fmt_vid_cap,
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001317 .vidioc_s_fmt_vid_cap = vpif_s_fmt_vid_cap,
1318 .vidioc_try_fmt_vid_cap = vpif_try_fmt_vid_cap,
Lad, Prabhakar7b4657f2014-05-16 10:33:49 -03001319
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001320 .vidioc_enum_input = vpif_enum_input,
1321 .vidioc_s_input = vpif_s_input,
1322 .vidioc_g_input = vpif_g_input,
Lad, Prabhakard9a3b132014-05-16 10:33:42 -03001323
1324 .vidioc_reqbufs = vb2_ioctl_reqbufs,
1325 .vidioc_create_bufs = vb2_ioctl_create_bufs,
1326 .vidioc_querybuf = vb2_ioctl_querybuf,
1327 .vidioc_qbuf = vb2_ioctl_qbuf,
1328 .vidioc_dqbuf = vb2_ioctl_dqbuf,
1329 .vidioc_expbuf = vb2_ioctl_expbuf,
1330 .vidioc_streamon = vb2_ioctl_streamon,
1331 .vidioc_streamoff = vb2_ioctl_streamoff,
1332
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001333 .vidioc_querystd = vpif_querystd,
Lad, Prabhakar7b4657f2014-05-16 10:33:49 -03001334 .vidioc_s_std = vpif_s_std,
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001335 .vidioc_g_std = vpif_g_std,
Lad, Prabhakard9a3b132014-05-16 10:33:42 -03001336
Lad, Prabhakar7b4657f2014-05-16 10:33:49 -03001337 .vidioc_enum_dv_timings = vpif_enum_dv_timings,
1338 .vidioc_query_dv_timings = vpif_query_dv_timings,
1339 .vidioc_s_dv_timings = vpif_s_dv_timings,
1340 .vidioc_g_dv_timings = vpif_g_dv_timings,
1341
Mats Randgaard7036d6a2010-12-16 12:17:41 -03001342 .vidioc_log_status = vpif_log_status,
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001343};
1344
1345/* vpif file operations */
1346static struct v4l2_file_operations vpif_fops = {
1347 .owner = THIS_MODULE,
Lad, Prabhakar7ff51192014-05-16 10:33:41 -03001348 .open = v4l2_fh_open,
1349 .release = vb2_fop_release,
Hans Verkuil46656af2011-01-04 06:51:35 -03001350 .unlocked_ioctl = video_ioctl2,
Lad, Prabhakard26d26b2014-05-16 10:33:40 -03001351 .mmap = vb2_fop_mmap,
1352 .poll = vb2_fop_poll
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001353};
1354
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001355/**
1356 * initialize_vpif() - Initialize vpif data structures
1357 *
1358 * Allocate memory for data structures and initialize them
1359 */
1360static int initialize_vpif(void)
1361{
Lad, Prabhakar4015bef2014-05-16 10:33:47 -03001362 int err, i, j;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001363 int free_channel_objects_index;
1364
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001365 /* Allocate memory for six channel objects */
1366 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
1367 vpif_obj.dev[i] =
1368 kzalloc(sizeof(*vpif_obj.dev[i]), GFP_KERNEL);
1369 /* If memory allocation fails, return error */
1370 if (!vpif_obj.dev[i]) {
1371 free_channel_objects_index = i;
1372 err = -ENOMEM;
1373 goto vpif_init_free_channel_objects;
1374 }
1375 }
1376 return 0;
1377
1378vpif_init_free_channel_objects:
1379 for (j = 0; j < free_channel_objects_index; j++)
1380 kfree(vpif_obj.dev[j]);
1381 return err;
1382}
1383
Lad, Prabhakar873229e2013-06-25 11:17:34 -03001384static int vpif_async_bound(struct v4l2_async_notifier *notifier,
1385 struct v4l2_subdev *subdev,
1386 struct v4l2_async_subdev *asd)
1387{
1388 int i;
1389
Kevin Hilman4a5f8ae2017-06-06 20:37:39 -03001390 for (i = 0; i < vpif_obj.config->asd_sizes[0]; i++) {
1391 struct v4l2_async_subdev *_asd = vpif_obj.config->asd[i];
1392 const struct device_node *node = _asd->match.of.node;
1393
1394 if (node == subdev->of_node) {
1395 vpif_obj.sd[i] = subdev;
1396 vpif_obj.config->chan_config->inputs[i].subdev_name =
1397 (char *)subdev->of_node->full_name;
1398 vpif_dbg(2, debug,
1399 "%s: setting input %d subdev_name = %s\n",
1400 __func__, i, subdev->of_node->full_name);
1401 return 0;
1402 }
1403 }
1404
Lad, Prabhakar873229e2013-06-25 11:17:34 -03001405 for (i = 0; i < vpif_obj.config->subdev_count; i++)
1406 if (!strcmp(vpif_obj.config->subdev_info[i].name,
1407 subdev->name)) {
1408 vpif_obj.sd[i] = subdev;
1409 return 0;
1410 }
1411
1412 return -EINVAL;
1413}
1414
1415static int vpif_probe_complete(void)
1416{
1417 struct common_obj *common;
Lad, Prabhakard26d26b2014-05-16 10:33:40 -03001418 struct video_device *vdev;
Lad, Prabhakar873229e2013-06-25 11:17:34 -03001419 struct channel_obj *ch;
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -03001420 struct vb2_queue *q;
Lad, Prabhakar8eef6662015-03-08 18:57:23 -03001421 int j, err, k;
Lad, Prabhakar873229e2013-06-25 11:17:34 -03001422
1423 for (j = 0; j < VPIF_CAPTURE_MAX_DEVICES; j++) {
1424 ch = vpif_obj.dev[j];
1425 ch->channel_id = j;
1426 common = &(ch->common[VPIF_VIDEO_INDEX]);
1427 spin_lock_init(&common->irqlock);
1428 mutex_init(&common->lock);
Lad, Prabhakar873229e2013-06-25 11:17:34 -03001429
1430 /* select input 0 */
1431 err = vpif_set_input(vpif_obj.config, ch, 0);
1432 if (err)
1433 goto probe_out;
1434
Lad, Prabhakara4965c52014-05-16 10:33:54 -03001435 /* set initial format */
1436 ch->video.stdid = V4L2_STD_525_60;
1437 memset(&ch->video.dv_timings, 0, sizeof(ch->video.dv_timings));
Kevin Hilmandde32d42017-06-06 20:37:40 -03001438 common->fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Lad, Prabhakara4965c52014-05-16 10:33:54 -03001439 vpif_update_std_info(ch);
1440
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -03001441 /* Initialize vb2 queue */
1442 q = &common->buffer_queue;
1443 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1444 q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
1445 q->drv_priv = ch;
1446 q->ops = &video_qops;
1447 q->mem_ops = &vb2_dma_contig_memops;
1448 q->buf_struct_size = sizeof(struct vpif_cap_buffer);
1449 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1450 q->min_buffers_needed = 1;
Lad, Prabhakar999ba692014-05-16 10:33:33 -03001451 q->lock = &common->lock;
Hans Verkuil53ddcc62016-02-15 13:09:10 -02001452 q->dev = vpif_dev;
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -03001453
1454 err = vb2_queue_init(q);
1455 if (err) {
1456 vpif_err("vpif_capture: vb2_queue_init() failed\n");
1457 goto probe_out;
1458 }
1459
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -03001460 INIT_LIST_HEAD(&common->dma_queue);
1461
Lad, Prabhakare75ea0c2014-05-16 10:33:46 -03001462 /* Initialize the video_device structure */
Lad, Prabhakar8eef6662015-03-08 18:57:23 -03001463 vdev = &ch->video_dev;
Lad, Prabhakare75ea0c2014-05-16 10:33:46 -03001464 strlcpy(vdev->name, VPIF_DRIVER_NAME, sizeof(vdev->name));
Lad, Prabhakar8eef6662015-03-08 18:57:23 -03001465 vdev->release = video_device_release_empty;
Lad, Prabhakare75ea0c2014-05-16 10:33:46 -03001466 vdev->fops = &vpif_fops;
1467 vdev->ioctl_ops = &vpif_ioctl_ops;
1468 vdev->v4l2_dev = &vpif_obj.v4l2_dev;
1469 vdev->vfl_dir = VFL_DIR_RX;
Lad, Prabhakard26d26b2014-05-16 10:33:40 -03001470 vdev->queue = q;
Lad, Prabhakar7ff51192014-05-16 10:33:41 -03001471 vdev->lock = &common->lock;
Lad, Prabhakar8eef6662015-03-08 18:57:23 -03001472 video_set_drvdata(&ch->video_dev, ch);
Lad, Prabhakard26d26b2014-05-16 10:33:40 -03001473 err = video_register_device(vdev,
Lad, Prabhakar873229e2013-06-25 11:17:34 -03001474 VFL_TYPE_GRABBER, (j ? 1 : 0));
1475 if (err)
1476 goto probe_out;
1477 }
1478
1479 v4l2_info(&vpif_obj.v4l2_dev, "VPIF capture driver initialized\n");
1480 return 0;
1481
1482probe_out:
1483 for (k = 0; k < j; k++) {
1484 /* Get the pointer to the channel object */
1485 ch = vpif_obj.dev[k];
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -03001486 common = &ch->common[k];
Lad, Prabhakar873229e2013-06-25 11:17:34 -03001487 /* Unregister video device */
Lad, Prabhakar8eef6662015-03-08 18:57:23 -03001488 video_unregister_device(&ch->video_dev);
Lad, Prabhakar873229e2013-06-25 11:17:34 -03001489 }
1490 kfree(vpif_obj.sd);
Lad, Prabhakar873229e2013-06-25 11:17:34 -03001491 v4l2_device_unregister(&vpif_obj.v4l2_dev);
1492
1493 return err;
1494}
1495
1496static int vpif_async_complete(struct v4l2_async_notifier *notifier)
1497{
1498 return vpif_probe_complete();
1499}
1500
Kevin Hilman4a5f8ae2017-06-06 20:37:39 -03001501static struct vpif_capture_config *
1502vpif_capture_get_pdata(struct platform_device *pdev)
1503{
1504 struct device_node *endpoint = NULL;
1505 struct v4l2_of_endpoint bus_cfg;
1506 struct vpif_capture_config *pdata;
1507 struct vpif_subdev_info *sdinfo;
1508 struct vpif_capture_chan_config *chan;
1509 unsigned int i;
1510
1511 /*
1512 * DT boot: OF node from parent device contains
1513 * video ports & endpoints data.
1514 */
1515 if (pdev->dev.parent && pdev->dev.parent->of_node)
1516 pdev->dev.of_node = pdev->dev.parent->of_node;
1517 if (!IS_ENABLED(CONFIG_OF) || !pdev->dev.of_node)
1518 return pdev->dev.platform_data;
1519
1520 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
1521 if (!pdata)
1522 return NULL;
1523 pdata->subdev_info =
1524 devm_kzalloc(&pdev->dev, sizeof(*pdata->subdev_info) *
1525 VPIF_CAPTURE_NUM_CHANNELS, GFP_KERNEL);
1526
1527 if (!pdata->subdev_info)
1528 return NULL;
1529
1530 for (i = 0; i < VPIF_CAPTURE_NUM_CHANNELS; i++) {
1531 struct device_node *rem;
1532 unsigned int flags;
1533 int err;
1534
1535 endpoint = of_graph_get_next_endpoint(pdev->dev.of_node,
1536 endpoint);
1537 if (!endpoint)
1538 break;
1539
1540 sdinfo = &pdata->subdev_info[i];
1541 chan = &pdata->chan_config[i];
1542 chan->inputs = devm_kzalloc(&pdev->dev,
1543 sizeof(*chan->inputs) *
1544 VPIF_CAPTURE_NUM_CHANNELS,
1545 GFP_KERNEL);
1546
1547 chan->input_count++;
1548 chan->inputs[i].input.type = V4L2_INPUT_TYPE_CAMERA;
1549 chan->inputs[i].input.std = V4L2_STD_ALL;
1550 chan->inputs[i].input.capabilities = V4L2_IN_CAP_STD;
1551
1552 err = v4l2_of_parse_endpoint(endpoint, &bus_cfg);
1553 if (err) {
1554 dev_err(&pdev->dev, "Could not parse the endpoint\n");
1555 goto done;
1556 }
1557 dev_dbg(&pdev->dev, "Endpoint %s, bus_width = %d\n",
1558 endpoint->full_name, bus_cfg.bus.parallel.bus_width);
1559 flags = bus_cfg.bus.parallel.flags;
1560
1561 if (flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH)
1562 chan->vpif_if.hd_pol = 1;
1563
1564 if (flags & V4L2_MBUS_VSYNC_ACTIVE_HIGH)
1565 chan->vpif_if.vd_pol = 1;
1566
1567 rem = of_graph_get_remote_port_parent(endpoint);
1568 if (!rem) {
1569 dev_dbg(&pdev->dev, "Remote device at %s not found\n",
1570 endpoint->full_name);
1571 goto done;
1572 }
1573
1574 dev_dbg(&pdev->dev, "Remote device %s, %s found\n",
1575 rem->name, rem->full_name);
1576 sdinfo->name = rem->full_name;
1577
1578 pdata->asd[i] = devm_kzalloc(&pdev->dev,
1579 sizeof(struct v4l2_async_subdev),
1580 GFP_KERNEL);
1581 if (!pdata->asd[i]) {
1582 of_node_put(rem);
1583 pdata = NULL;
1584 goto done;
1585 }
1586
1587 pdata->asd[i]->match_type = V4L2_ASYNC_MATCH_OF;
1588 pdata->asd[i]->match.of.node = rem;
1589 of_node_put(rem);
1590 }
1591
1592done:
1593 pdata->asd_sizes[0] = i;
1594 pdata->subdev_count = i;
1595 pdata->card_name = "DA850/OMAP-L138 Video Capture";
1596
1597 return pdata;
1598}
1599
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001600/**
1601 * vpif_probe : This function probes the vpif capture driver
1602 * @pdev: platform device pointer
1603 *
1604 * This creates device entries by register itself to the V4L2 driver and
1605 * initializes fields of each channel objects
1606 */
1607static __init int vpif_probe(struct platform_device *pdev)
1608{
1609 struct vpif_subdev_info *subdevdata;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001610 struct i2c_adapter *i2c_adap;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001611 struct resource *res;
1612 int subdev_count;
Lad, Prabhakar8eef6662015-03-08 18:57:23 -03001613 int res_idx = 0;
1614 int i, err;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001615
Kevin Hilman4a5f8ae2017-06-06 20:37:39 -03001616 pdev->dev.platform_data = vpif_capture_get_pdata(pdev);
1617 if (!pdev->dev.platform_data) {
1618 dev_warn(&pdev->dev, "Missing platform data. Giving up.\n");
1619 return -EINVAL;
1620 }
1621
Kevin Hilmanbff782d2016-12-16 22:47:54 -02001622 if (!pdev->dev.platform_data) {
1623 dev_warn(&pdev->dev, "Missing platform data. Giving up.\n");
1624 return -EINVAL;
1625 }
1626
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001627 vpif_dev = &pdev->dev;
1628
1629 err = initialize_vpif();
1630 if (err) {
1631 v4l2_err(vpif_dev->driver, "Error initializing vpif\n");
1632 return err;
1633 }
1634
Hans Verkuild2f7a1a2011-12-13 05:44:42 -03001635 err = v4l2_device_register(vpif_dev, &vpif_obj.v4l2_dev);
1636 if (err) {
1637 v4l2_err(vpif_dev->driver, "Error registering v4l2 device\n");
1638 return err;
1639 }
1640
Hans Verkuil5be452c2012-09-20 09:06:29 -03001641 while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, res_idx))) {
Lad, Prabhakara76a0b32013-06-17 11:20:47 -03001642 err = devm_request_irq(&pdev->dev, res->start, vpif_channel_isr,
Lad, Prabhakare75ea0c2014-05-16 10:33:46 -03001643 IRQF_SHARED, VPIF_DRIVER_NAME,
Lad, Prabhakara76a0b32013-06-17 11:20:47 -03001644 (void *)(&vpif_obj.dev[res_idx]->
1645 channel_id));
1646 if (err) {
1647 err = -EINVAL;
1648 goto vpif_unregister;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001649 }
Hans Verkuil5be452c2012-09-20 09:06:29 -03001650 res_idx++;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001651 }
1652
Lad, Prabhakar873229e2013-06-25 11:17:34 -03001653 vpif_obj.config = pdev->dev.platform_data;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001654
Lad, Prabhakar873229e2013-06-25 11:17:34 -03001655 subdev_count = vpif_obj.config->subdev_count;
Markus Elfringb442c462016-10-12 10:15:34 -03001656 vpif_obj.sd = kcalloc(subdev_count, sizeof(*vpif_obj.sd), GFP_KERNEL);
Markus Elfring03161cd2016-10-12 10:20:34 -03001657 if (!vpif_obj.sd) {
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001658 err = -ENOMEM;
Lad, Prabhakar8eef6662015-03-08 18:57:23 -03001659 goto vpif_unregister;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001660 }
1661
Kevin Hilman4a5f8ae2017-06-06 20:37:39 -03001662 if (!vpif_obj.config->asd_sizes[0]) {
Kevin Hilman2d40cb32016-12-07 16:30:22 -02001663 int i2c_id = vpif_obj.config->i2c_adapter_id;
1664
1665 i2c_adap = i2c_get_adapter(i2c_id);
1666 WARN_ON(!i2c_adap);
Lad, Prabhakar873229e2013-06-25 11:17:34 -03001667 for (i = 0; i < subdev_count; i++) {
1668 subdevdata = &vpif_obj.config->subdev_info[i];
1669 vpif_obj.sd[i] =
1670 v4l2_i2c_new_subdev_board(&vpif_obj.v4l2_dev,
1671 i2c_adap,
1672 &subdevdata->
1673 board_info,
1674 NULL);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001675
Lad, Prabhakar873229e2013-06-25 11:17:34 -03001676 if (!vpif_obj.sd[i]) {
1677 vpif_err("Error registering v4l2 subdevice\n");
Wei Yongjun2fcd9dc2013-09-02 05:06:10 -03001678 err = -ENODEV;
Lad, Prabhakar873229e2013-06-25 11:17:34 -03001679 goto probe_subdev_out;
1680 }
1681 v4l2_info(&vpif_obj.v4l2_dev,
1682 "registered sub device %s\n",
1683 subdevdata->name);
1684 }
1685 vpif_probe_complete();
1686 } else {
Sylwester Nawrockie8419d02013-07-19 12:31:10 -03001687 vpif_obj.notifier.subdevs = vpif_obj.config->asd;
Lad, Prabhakar873229e2013-06-25 11:17:34 -03001688 vpif_obj.notifier.num_subdevs = vpif_obj.config->asd_sizes[0];
1689 vpif_obj.notifier.bound = vpif_async_bound;
1690 vpif_obj.notifier.complete = vpif_async_complete;
1691 err = v4l2_async_notifier_register(&vpif_obj.v4l2_dev,
1692 &vpif_obj.notifier);
1693 if (err) {
1694 vpif_err("Error registering async notifier\n");
1695 err = -EINVAL;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001696 goto probe_subdev_out;
1697 }
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001698 }
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001699
1700 return 0;
1701
Hans Verkuilb65814e2012-09-20 09:06:26 -03001702probe_subdev_out:
1703 /* free sub devices memory */
1704 kfree(vpif_obj.sd);
Lad, Prabhakarb2de4f22013-06-17 11:20:46 -03001705vpif_unregister:
Hans Verkuild2f7a1a2011-12-13 05:44:42 -03001706 v4l2_device_unregister(&vpif_obj.v4l2_dev);
Lad, Prabhakarb2de4f22013-06-17 11:20:46 -03001707
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001708 return err;
1709}
1710
1711/**
1712 * vpif_remove() - driver remove handler
1713 * @device: ptr to platform device structure
1714 *
1715 * The vidoe device is unregistered
1716 */
1717static int vpif_remove(struct platform_device *device)
1718{
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -03001719 struct common_obj *common;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001720 struct channel_obj *ch;
Lad, Prabhakarb2de4f22013-06-17 11:20:46 -03001721 int i;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001722
1723 v4l2_device_unregister(&vpif_obj.v4l2_dev);
1724
Lad, Prabhakar410ca602013-06-17 11:20:44 -03001725 kfree(vpif_obj.sd);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001726 /* un-register device */
1727 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
1728 /* Get the pointer to the channel object */
1729 ch = vpif_obj.dev[i];
Prabhakar Lade9b58722014-07-18 13:31:51 -03001730 common = &ch->common[VPIF_VIDEO_INDEX];
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001731 /* Unregister video device */
Lad, Prabhakar8eef6662015-03-08 18:57:23 -03001732 video_unregister_device(&ch->video_dev);
Lad, Prabhakar410ca602013-06-17 11:20:44 -03001733 kfree(vpif_obj.dev[i]);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001734 }
1735 return 0;
1736}
1737
Lad, Prabhakarb7047712014-05-16 10:33:50 -03001738#ifdef CONFIG_PM_SLEEP
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001739/**
1740 * vpif_suspend: vpif device suspend
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001741 */
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03001742static int vpif_suspend(struct device *dev)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001743{
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03001744
1745 struct common_obj *common;
1746 struct channel_obj *ch;
1747 int i;
1748
1749 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
1750 /* Get the pointer to the channel object */
1751 ch = vpif_obj.dev[i];
1752 common = &ch->common[VPIF_VIDEO_INDEX];
Lad, Prabhakarb7047712014-05-16 10:33:50 -03001753
Prabhakar Ladc54d4a02014-09-06 12:26:51 -03001754 if (!vb2_start_streaming_called(&common->buffer_queue))
Lad, Prabhakarb7047712014-05-16 10:33:50 -03001755 continue;
1756
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03001757 mutex_lock(&common->lock);
Lad, Prabhakarb7047712014-05-16 10:33:50 -03001758 /* Disable channel */
1759 if (ch->channel_id == VPIF_CHANNEL0_VIDEO) {
1760 enable_channel0(0);
1761 channel0_intr_enable(0);
1762 }
1763 if (ch->channel_id == VPIF_CHANNEL1_VIDEO ||
1764 ycmux_mode == 2) {
1765 enable_channel1(0);
1766 channel1_intr_enable(0);
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03001767 }
1768 mutex_unlock(&common->lock);
1769 }
1770
1771 return 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001772}
1773
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03001774/*
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001775 * vpif_resume: vpif device suspend
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001776 */
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03001777static int vpif_resume(struct device *dev)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001778{
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03001779 struct common_obj *common;
1780 struct channel_obj *ch;
1781 int i;
1782
1783 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
1784 /* Get the pointer to the channel object */
1785 ch = vpif_obj.dev[i];
1786 common = &ch->common[VPIF_VIDEO_INDEX];
Lad, Prabhakarb7047712014-05-16 10:33:50 -03001787
Prabhakar Ladc54d4a02014-09-06 12:26:51 -03001788 if (!vb2_start_streaming_called(&common->buffer_queue))
Lad, Prabhakarb7047712014-05-16 10:33:50 -03001789 continue;
1790
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03001791 mutex_lock(&common->lock);
Lad, Prabhakarb7047712014-05-16 10:33:50 -03001792 /* Enable channel */
1793 if (ch->channel_id == VPIF_CHANNEL0_VIDEO) {
1794 enable_channel0(1);
1795 channel0_intr_enable(1);
1796 }
1797 if (ch->channel_id == VPIF_CHANNEL1_VIDEO ||
1798 ycmux_mode == 2) {
1799 enable_channel1(1);
1800 channel1_intr_enable(1);
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03001801 }
1802 mutex_unlock(&common->lock);
1803 }
1804
1805 return 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001806}
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03001807#endif
1808
Lad, Prabhakarb7047712014-05-16 10:33:50 -03001809static SIMPLE_DEV_PM_OPS(vpif_pm_ops, vpif_suspend, vpif_resume);
1810
Mats Randgaardffa1b392010-08-30 10:30:36 -03001811static __refdata struct platform_driver vpif_driver = {
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001812 .driver = {
Lad, Prabhakare75ea0c2014-05-16 10:33:46 -03001813 .name = VPIF_DRIVER_NAME,
Lad, Prabhakarb7047712014-05-16 10:33:50 -03001814 .pm = &vpif_pm_ops,
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001815 },
1816 .probe = vpif_probe,
1817 .remove = vpif_remove,
1818};
1819
Lad, Prabhakarfe424b22013-06-17 11:20:45 -03001820module_platform_driver(vpif_driver);