blob: 753411cbbc9a7c3053b063530726f99aa0f7bcf8 [file] [log] [blame]
Federico Vagaefeb98b2012-04-12 12:39:38 -03001/*
2 * This is the driver for the STA2x11 Video Input Port.
3 *
Federico Vaga8dc97ea2013-02-05 14:34:37 -03004 * Copyright (C) 2012 ST Microelectronics
5 * author: Federico Vaga <federico.vaga@gmail.com>
Federico Vagaefeb98b2012-04-12 12:39:38 -03006 * Copyright (C) 2010 WindRiver Systems, Inc.
Federico Vaga8dc97ea2013-02-05 14:34:37 -03007 * authors: Andreas Kies <andreas.kies@windriver.com>
8 * Vlad Lungu <vlad.lungu@windriver.com>
Federico Vagaefeb98b2012-04-12 12:39:38 -03009 *
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms and conditions of the GNU General Public License,
12 * version 2, as published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope it will be useful, but WITHOUT
15 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
17 * more details.
18 *
19 * You should have received a copy of the GNU General Public License along with
20 * this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
22 *
23 * The full GNU General Public License is included in this distribution in
24 * the file called "COPYING".
25 *
Federico Vagaefeb98b2012-04-12 12:39:38 -030026 */
27
28#include <linux/types.h>
29#include <linux/kernel.h>
30#include <linux/module.h>
31#include <linux/init.h>
Federico Vagaefeb98b2012-04-12 12:39:38 -030032#include <linux/videodev2.h>
Federico Vagaefeb98b2012-04-12 12:39:38 -030033#include <linux/kmod.h>
Federico Vagaefeb98b2012-04-12 12:39:38 -030034#include <linux/pci.h>
35#include <linux/interrupt.h>
Federico Vagaefeb98b2012-04-12 12:39:38 -030036#include <linux/io.h>
37#include <linux/gpio.h>
38#include <linux/i2c.h>
39#include <linux/delay.h>
40#include <media/v4l2-common.h>
41#include <media/v4l2-device.h>
Federico Vaga8dc97ea2013-02-05 14:34:37 -030042#include <media/v4l2-ctrls.h>
Federico Vagaefeb98b2012-04-12 12:39:38 -030043#include <media/v4l2-ioctl.h>
Federico Vaga8dc97ea2013-02-05 14:34:37 -030044#include <media/v4l2-fh.h>
45#include <media/v4l2-event.h>
46#include <media/videobuf2-dma-contig.h>
Federico Vagaefeb98b2012-04-12 12:39:38 -030047
48#include "sta2x11_vip.h"
49
Federico Vagaefeb98b2012-04-12 12:39:38 -030050#define DRV_VERSION "1.3"
51
52#ifndef PCI_DEVICE_ID_STMICRO_VIP
53#define PCI_DEVICE_ID_STMICRO_VIP 0xCC0D
54#endif
55
56#define MAX_FRAMES 4
57
58/*Register offsets*/
59#define DVP_CTL 0x00
60#define DVP_TFO 0x04
61#define DVP_TFS 0x08
62#define DVP_BFO 0x0C
63#define DVP_BFS 0x10
Federico Vaga8dc97ea2013-02-05 14:34:37 -030064#define DVP_VTP 0x14
65#define DVP_VBP 0x18
Federico Vagaefeb98b2012-04-12 12:39:38 -030066#define DVP_VMP 0x1C
67#define DVP_ITM 0x98
68#define DVP_ITS 0x9C
69#define DVP_STA 0xA0
70#define DVP_HLFLN 0xA8
71#define DVP_RGB 0xC0
72#define DVP_PKZ 0xF0
73
74/*Register fields*/
75#define DVP_CTL_ENA 0x00000001
76#define DVP_CTL_RST 0x80000000
77#define DVP_CTL_DIS (~0x00040001)
78
79#define DVP_IT_VSB 0x00000008
80#define DVP_IT_VST 0x00000010
81#define DVP_IT_FIFO 0x00000020
82
83#define DVP_HLFLN_SD 0x00000001
84
Federico Vagaefeb98b2012-04-12 12:39:38 -030085#define SAVE_COUNT 8
86#define AUX_COUNT 3
87#define IRQ_COUNT 1
88
Federico Vaga8dc97ea2013-02-05 14:34:37 -030089
90struct vip_buffer {
Junghak Sung2d700712015-09-22 10:30:30 -030091 struct vb2_v4l2_buffer vb;
Federico Vaga8dc97ea2013-02-05 14:34:37 -030092 struct list_head list;
93 dma_addr_t dma;
94};
Junghak Sung2d700712015-09-22 10:30:30 -030095static inline struct vip_buffer *to_vip_buffer(struct vb2_v4l2_buffer *vb2)
Federico Vaga8dc97ea2013-02-05 14:34:37 -030096{
97 return container_of(vb2, struct vip_buffer, vb);
98}
99
Federico Vagaefeb98b2012-04-12 12:39:38 -0300100/**
101 * struct sta2x11_vip - All internal data for one instance of device
102 * @v4l2_dev: device registered in v4l layer
103 * @video_dev: properties of our device
104 * @pdev: PCI device
105 * @adapter: contains I2C adapter information
106 * @register_save_area: All relevant register are saved here during suspend
107 * @decoder: contains information about video DAC
Federico Vaga8dc97ea2013-02-05 14:34:37 -0300108 * @ctrl_hdl: handler for control framework
Federico Vagaefeb98b2012-04-12 12:39:38 -0300109 * @format: pixel format, fixed UYVY
110 * @std: video standard (e.g. PAL/NTSC)
111 * @input: input line for video signal ( 0 or 1 )
Federico Vagaefeb98b2012-04-12 12:39:38 -0300112 * @disabled: Device is in power down state
Federico Vagaefeb98b2012-04-12 12:39:38 -0300113 * @slock: for excluse acces of registers
Federico Vaga8dc97ea2013-02-05 14:34:37 -0300114 * @alloc_ctx: context for videobuf2
115 * @vb_vidq: queue maintained by videobuf2 layer
116 * @buffer_list: list of buffer in use
117 * @sequence: sequence number of acquired buffer
118 * @active: current active buffer
119 * @lock: used in videobuf2 callback
Federico Vagaefeb98b2012-04-12 12:39:38 -0300120 * @tcount: Number of top frames
121 * @bcount: Number of bottom frames
122 * @overflow: Number of FIFO overflows
Federico Vagaefeb98b2012-04-12 12:39:38 -0300123 * @iomem: hardware base address
124 * @config: I2C and gpio config from platform
125 *
126 * All non-local data is accessed via this structure.
127 */
Federico Vagaefeb98b2012-04-12 12:39:38 -0300128struct sta2x11_vip {
129 struct v4l2_device v4l2_dev;
Hans Verkuil4db4ca742015-03-09 13:34:04 -0300130 struct video_device video_dev;
Federico Vagaefeb98b2012-04-12 12:39:38 -0300131 struct pci_dev *pdev;
132 struct i2c_adapter *adapter;
133 unsigned int register_save_area[IRQ_COUNT + SAVE_COUNT + AUX_COUNT];
134 struct v4l2_subdev *decoder;
Federico Vaga8dc97ea2013-02-05 14:34:37 -0300135 struct v4l2_ctrl_handler ctrl_hdl;
136
137
Federico Vagaefeb98b2012-04-12 12:39:38 -0300138 struct v4l2_pix_format format;
139 v4l2_std_id std;
140 unsigned int input;
Federico Vagaefeb98b2012-04-12 12:39:38 -0300141 int disabled;
Federico Vaga8dc97ea2013-02-05 14:34:37 -0300142 spinlock_t slock;
143
144 struct vb2_alloc_ctx *alloc_ctx;
145 struct vb2_queue vb_vidq;
146 struct list_head buffer_list;
147 unsigned int sequence;
148 struct vip_buffer *active; /* current active buffer */
149 spinlock_t lock; /* Used in videobuf2 callback */
150
151 /* Interrupt counters */
152 int tcount, bcount;
Federico Vagaefeb98b2012-04-12 12:39:38 -0300153 int overflow;
Federico Vaga8dc97ea2013-02-05 14:34:37 -0300154
Mauro Carvalho Chehab87f4ebc2014-09-24 11:17:06 -0300155 void __iomem *iomem; /* I/O Memory */
Federico Vagaefeb98b2012-04-12 12:39:38 -0300156 struct vip_config *config;
157};
158
159static const unsigned int registers_to_save[AUX_COUNT] = {
160 DVP_HLFLN, DVP_RGB, DVP_PKZ
161};
162
163static struct v4l2_pix_format formats_50[] = {
164 { /*PAL interlaced */
165 .width = 720,
166 .height = 576,
167 .pixelformat = V4L2_PIX_FMT_UYVY,
168 .field = V4L2_FIELD_INTERLACED,
169 .bytesperline = 720 * 2,
170 .sizeimage = 720 * 2 * 576,
171 .colorspace = V4L2_COLORSPACE_SMPTE170M},
172 { /*PAL top */
173 .width = 720,
174 .height = 288,
175 .pixelformat = V4L2_PIX_FMT_UYVY,
176 .field = V4L2_FIELD_TOP,
177 .bytesperline = 720 * 2,
178 .sizeimage = 720 * 2 * 288,
179 .colorspace = V4L2_COLORSPACE_SMPTE170M},
180 { /*PAL bottom */
181 .width = 720,
182 .height = 288,
183 .pixelformat = V4L2_PIX_FMT_UYVY,
184 .field = V4L2_FIELD_BOTTOM,
185 .bytesperline = 720 * 2,
186 .sizeimage = 720 * 2 * 288,
187 .colorspace = V4L2_COLORSPACE_SMPTE170M},
188
189};
190
191static struct v4l2_pix_format formats_60[] = {
192 { /*NTSC interlaced */
193 .width = 720,
194 .height = 480,
195 .pixelformat = V4L2_PIX_FMT_UYVY,
196 .field = V4L2_FIELD_INTERLACED,
197 .bytesperline = 720 * 2,
198 .sizeimage = 720 * 2 * 480,
199 .colorspace = V4L2_COLORSPACE_SMPTE170M},
200 { /*NTSC top */
201 .width = 720,
202 .height = 240,
203 .pixelformat = V4L2_PIX_FMT_UYVY,
204 .field = V4L2_FIELD_TOP,
205 .bytesperline = 720 * 2,
206 .sizeimage = 720 * 2 * 240,
207 .colorspace = V4L2_COLORSPACE_SMPTE170M},
208 { /*NTSC bottom */
209 .width = 720,
210 .height = 240,
211 .pixelformat = V4L2_PIX_FMT_UYVY,
212 .field = V4L2_FIELD_BOTTOM,
213 .bytesperline = 720 * 2,
214 .sizeimage = 720 * 2 * 240,
215 .colorspace = V4L2_COLORSPACE_SMPTE170M},
216};
217
Federico Vaga8dc97ea2013-02-05 14:34:37 -0300218/* Write VIP register */
219static inline void reg_write(struct sta2x11_vip *vip, unsigned int reg, u32 val)
Federico Vagaefeb98b2012-04-12 12:39:38 -0300220{
Federico Vaga8dc97ea2013-02-05 14:34:37 -0300221 iowrite32((val), (vip->iomem)+(reg));
222}
223/* Read VIP register */
224static inline u32 reg_read(struct sta2x11_vip *vip, unsigned int reg)
Federico Vagaefeb98b2012-04-12 12:39:38 -0300225{
Federico Vaga8dc97ea2013-02-05 14:34:37 -0300226 return ioread32((vip->iomem)+(reg));
227}
228/* Start DMA acquisition */
229static void start_dma(struct sta2x11_vip *vip, struct vip_buffer *vip_buf)
230{
231 unsigned long offset = 0;
Federico Vagaefeb98b2012-04-12 12:39:38 -0300232
Federico Vaga8dc97ea2013-02-05 14:34:37 -0300233 if (vip->format.field == V4L2_FIELD_INTERLACED)
234 offset = vip->format.width * 2;
Federico Vagaefeb98b2012-04-12 12:39:38 -0300235
Federico Vaga8dc97ea2013-02-05 14:34:37 -0300236 spin_lock_irq(&vip->slock);
237 /* Enable acquisition */
238 reg_write(vip, DVP_CTL, reg_read(vip, DVP_CTL) | DVP_CTL_ENA);
239 /* Set Top and Bottom Field memory address */
240 reg_write(vip, DVP_VTP, (u32)vip_buf->dma);
241 reg_write(vip, DVP_VBP, (u32)vip_buf->dma + offset);
242 spin_unlock_irq(&vip->slock);
Federico Vagaefeb98b2012-04-12 12:39:38 -0300243}
244
Federico Vaga8dc97ea2013-02-05 14:34:37 -0300245/* Fetch the next buffer to activate */
246static void vip_active_buf_next(struct sta2x11_vip *vip)
Federico Vagaefeb98b2012-04-12 12:39:38 -0300247{
Federico Vaga8dc97ea2013-02-05 14:34:37 -0300248 /* Get the next buffer */
249 spin_lock(&vip->lock);
250 if (list_empty(&vip->buffer_list)) {/* No available buffer */
251 spin_unlock(&vip->lock);
Federico Vagaefeb98b2012-04-12 12:39:38 -0300252 return;
253 }
Federico Vaga8dc97ea2013-02-05 14:34:37 -0300254 vip->active = list_first_entry(&vip->buffer_list,
255 struct vip_buffer,
256 list);
257 /* Reset Top and Bottom counter */
Federico Vagaefeb98b2012-04-12 12:39:38 -0300258 vip->tcount = 0;
259 vip->bcount = 0;
Federico Vaga8dc97ea2013-02-05 14:34:37 -0300260 spin_unlock(&vip->lock);
261 if (vb2_is_streaming(&vip->vb_vidq)) { /* streaming is on */
262 start_dma(vip, vip->active); /* start dma capture */
Federico Vagaefeb98b2012-04-12 12:39:38 -0300263 }
Federico Vagaefeb98b2012-04-12 12:39:38 -0300264}
265
Federico Vaga8dc97ea2013-02-05 14:34:37 -0300266
267/* Videobuf2 Operations */
Hans Verkuildf9ecb02015-10-28 00:50:37 -0200268static int queue_setup(struct vb2_queue *vq,
Federico Vaga8dc97ea2013-02-05 14:34:37 -0300269 unsigned int *nbuffers, unsigned int *nplanes,
270 unsigned int sizes[], void *alloc_ctxs[])
Federico Vagaefeb98b2012-04-12 12:39:38 -0300271{
Federico Vaga8dc97ea2013-02-05 14:34:37 -0300272 struct sta2x11_vip *vip = vb2_get_drv_priv(vq);
Federico Vagaefeb98b2012-04-12 12:39:38 -0300273
Federico Vaga8dc97ea2013-02-05 14:34:37 -0300274 if (!(*nbuffers) || *nbuffers < MAX_FRAMES)
275 *nbuffers = MAX_FRAMES;
Federico Vagaefeb98b2012-04-12 12:39:38 -0300276
Federico Vaga8dc97ea2013-02-05 14:34:37 -0300277 *nplanes = 1;
278 sizes[0] = vip->format.sizeimage;
279 alloc_ctxs[0] = vip->alloc_ctx;
Federico Vagaefeb98b2012-04-12 12:39:38 -0300280
Federico Vaga8dc97ea2013-02-05 14:34:37 -0300281 vip->sequence = 0;
Federico Vagaefeb98b2012-04-12 12:39:38 -0300282 vip->active = NULL;
Federico Vaga8dc97ea2013-02-05 14:34:37 -0300283 vip->tcount = 0;
284 vip->bcount = 0;
Federico Vagaefeb98b2012-04-12 12:39:38 -0300285
Federico Vaga8dc97ea2013-02-05 14:34:37 -0300286 return 0;
287};
288static int buffer_init(struct vb2_buffer *vb)
289{
Junghak Sung2d700712015-09-22 10:30:30 -0300290 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
291 struct vip_buffer *vip_buf = to_vip_buffer(vbuf);
Federico Vagaefeb98b2012-04-12 12:39:38 -0300292
Federico Vaga8dc97ea2013-02-05 14:34:37 -0300293 vip_buf->dma = vb2_dma_contig_plane_dma_addr(vb, 0);
294 INIT_LIST_HEAD(&vip_buf->list);
Federico Vagaefeb98b2012-04-12 12:39:38 -0300295 return 0;
296}
297
Federico Vaga8dc97ea2013-02-05 14:34:37 -0300298static int buffer_prepare(struct vb2_buffer *vb)
Federico Vagaefeb98b2012-04-12 12:39:38 -0300299{
Junghak Sung2d700712015-09-22 10:30:30 -0300300 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
Federico Vaga8dc97ea2013-02-05 14:34:37 -0300301 struct sta2x11_vip *vip = vb2_get_drv_priv(vb->vb2_queue);
Junghak Sung2d700712015-09-22 10:30:30 -0300302 struct vip_buffer *vip_buf = to_vip_buffer(vbuf);
Federico Vaga8dc97ea2013-02-05 14:34:37 -0300303 unsigned long size;
Federico Vagaefeb98b2012-04-12 12:39:38 -0300304
Federico Vaga8dc97ea2013-02-05 14:34:37 -0300305 size = vip->format.sizeimage;
306 if (vb2_plane_size(vb, 0) < size) {
307 v4l2_err(&vip->v4l2_dev, "buffer too small (%lu < %lu)\n",
308 vb2_plane_size(vb, 0), size);
309 return -EINVAL;
310 }
311
Junghak Sung2d700712015-09-22 10:30:30 -0300312 vb2_set_plane_payload(&vip_buf->vb.vb2_buf, 0, size);
Federico Vaga8dc97ea2013-02-05 14:34:37 -0300313
314 return 0;
315}
316static void buffer_queue(struct vb2_buffer *vb)
317{
Junghak Sung2d700712015-09-22 10:30:30 -0300318 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
Federico Vaga8dc97ea2013-02-05 14:34:37 -0300319 struct sta2x11_vip *vip = vb2_get_drv_priv(vb->vb2_queue);
Junghak Sung2d700712015-09-22 10:30:30 -0300320 struct vip_buffer *vip_buf = to_vip_buffer(vbuf);
Federico Vaga8dc97ea2013-02-05 14:34:37 -0300321
322 spin_lock(&vip->lock);
323 list_add_tail(&vip_buf->list, &vip->buffer_list);
324 if (!vip->active) { /* No active buffer, active the first one */
325 vip->active = list_first_entry(&vip->buffer_list,
326 struct vip_buffer,
327 list);
328 if (vb2_is_streaming(&vip->vb_vidq)) /* streaming is on */
329 start_dma(vip, vip_buf); /* start dma capture */
330 }
331 spin_unlock(&vip->lock);
332}
Hans Verkuil06470642014-03-04 07:27:13 -0300333static void buffer_finish(struct vb2_buffer *vb)
Federico Vaga8dc97ea2013-02-05 14:34:37 -0300334{
Junghak Sung2d700712015-09-22 10:30:30 -0300335 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
Federico Vaga8dc97ea2013-02-05 14:34:37 -0300336 struct sta2x11_vip *vip = vb2_get_drv_priv(vb->vb2_queue);
Junghak Sung2d700712015-09-22 10:30:30 -0300337 struct vip_buffer *vip_buf = to_vip_buffer(vbuf);
Federico Vaga8dc97ea2013-02-05 14:34:37 -0300338
339 /* Buffer handled, remove it from the list */
340 spin_lock(&vip->lock);
341 list_del_init(&vip_buf->list);
342 spin_unlock(&vip->lock);
343
Hans Verkuil9c0863b2014-03-04 07:34:49 -0300344 if (vb2_is_streaming(vb->vb2_queue))
345 vip_active_buf_next(vip);
Federico Vaga8dc97ea2013-02-05 14:34:37 -0300346}
347
348static int start_streaming(struct vb2_queue *vq, unsigned int count)
349{
350 struct sta2x11_vip *vip = vb2_get_drv_priv(vq);
351
Federico Vagaefeb98b2012-04-12 12:39:38 -0300352 spin_lock_irq(&vip->slock);
Federico Vaga8dc97ea2013-02-05 14:34:37 -0300353 /* Enable interrupt VSYNC Top and Bottom*/
354 reg_write(vip, DVP_ITM, DVP_IT_VSB | DVP_IT_VST);
Federico Vagaefeb98b2012-04-12 12:39:38 -0300355 spin_unlock_irq(&vip->slock);
356
Federico Vaga8dc97ea2013-02-05 14:34:37 -0300357 if (count)
358 start_dma(vip, vip->active);
Federico Vagaefeb98b2012-04-12 12:39:38 -0300359
Federico Vagaefeb98b2012-04-12 12:39:38 -0300360 return 0;
361}
362
Federico Vaga8dc97ea2013-02-05 14:34:37 -0300363/* abort streaming and wait for last buffer */
Hans Verkuile37559b2014-04-17 02:47:21 -0300364static void stop_streaming(struct vb2_queue *vq)
Federico Vagaefeb98b2012-04-12 12:39:38 -0300365{
Federico Vaga8dc97ea2013-02-05 14:34:37 -0300366 struct sta2x11_vip *vip = vb2_get_drv_priv(vq);
367 struct vip_buffer *vip_buf, *node;
Federico Vagaefeb98b2012-04-12 12:39:38 -0300368
Federico Vaga8dc97ea2013-02-05 14:34:37 -0300369 /* Disable acquisition */
370 reg_write(vip, DVP_CTL, reg_read(vip, DVP_CTL) & ~DVP_CTL_ENA);
371 /* Disable all interrupts */
372 reg_write(vip, DVP_ITM, 0);
373
374 /* Release all active buffers */
375 spin_lock(&vip->lock);
376 list_for_each_entry_safe(vip_buf, node, &vip->buffer_list, list) {
Junghak Sung2d700712015-09-22 10:30:30 -0300377 vb2_buffer_done(&vip_buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
Federico Vaga8dc97ea2013-02-05 14:34:37 -0300378 list_del(&vip_buf->list);
379 }
380 spin_unlock(&vip->lock);
Federico Vagaefeb98b2012-04-12 12:39:38 -0300381}
382
Federico Vaga8dc97ea2013-02-05 14:34:37 -0300383static struct vb2_ops vip_video_qops = {
384 .queue_setup = queue_setup,
385 .buf_init = buffer_init,
386 .buf_prepare = buffer_prepare,
387 .buf_finish = buffer_finish,
388 .buf_queue = buffer_queue,
389 .start_streaming = start_streaming,
390 .stop_streaming = stop_streaming,
391};
Federico Vagaefeb98b2012-04-12 12:39:38 -0300392
Federico Vagaefeb98b2012-04-12 12:39:38 -0300393
Federico Vaga8dc97ea2013-02-05 14:34:37 -0300394/* File Operations */
395static const struct v4l2_file_operations vip_fops = {
396 .owner = THIS_MODULE,
397 .open = v4l2_fh_open,
398 .release = vb2_fop_release,
399 .unlocked_ioctl = video_ioctl2,
400 .read = vb2_fop_read,
401 .mmap = vb2_fop_mmap,
402 .poll = vb2_fop_poll
403};
Federico Vagaefeb98b2012-04-12 12:39:38 -0300404
Federico Vagaefeb98b2012-04-12 12:39:38 -0300405
406/**
407 * vidioc_querycap - return capabilities of device
Federico Vaga8dc97ea2013-02-05 14:34:37 -0300408 * @file: descriptor of device
Federico Vagaefeb98b2012-04-12 12:39:38 -0300409 * @cap: contains return values
410 *
411 * the capabilities of the device are returned
412 *
413 * return value: 0, no error.
414 */
415static int vidioc_querycap(struct file *file, void *priv,
416 struct v4l2_capability *cap)
417{
Federico Vaga8dc97ea2013-02-05 14:34:37 -0300418 struct sta2x11_vip *vip = video_drvdata(file);
Federico Vagaefeb98b2012-04-12 12:39:38 -0300419
Federico Vaga8dc97ea2013-02-05 14:34:37 -0300420 strcpy(cap->driver, KBUILD_MODNAME);
421 strcpy(cap->card, KBUILD_MODNAME);
Federico Vagaefeb98b2012-04-12 12:39:38 -0300422 snprintf(cap->bus_info, sizeof(cap->bus_info), "PCI:%s",
423 pci_name(vip->pdev));
Federico Vaga8dc97ea2013-02-05 14:34:37 -0300424 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE |
425 V4L2_CAP_STREAMING;
426 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
Federico Vagaefeb98b2012-04-12 12:39:38 -0300427
428 return 0;
429}
430
431/**
432 * vidioc_s_std - set video standard
Federico Vaga8dc97ea2013-02-05 14:34:37 -0300433 * @file: descriptor of device
Federico Vagaefeb98b2012-04-12 12:39:38 -0300434 * @std: contains standard to be set
435 *
436 * the video standard is set
437 *
438 * return value: 0, no error.
439 *
440 * -EIO, no input signal detected
441 *
442 * other, returned from video DAC.
443 */
Hans Verkuil314527a2013-03-15 06:10:40 -0300444static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id std)
Federico Vagaefeb98b2012-04-12 12:39:38 -0300445{
Federico Vaga8dc97ea2013-02-05 14:34:37 -0300446 struct sta2x11_vip *vip = video_drvdata(file);
Federico Vagaefeb98b2012-04-12 12:39:38 -0300447 v4l2_std_id oldstd = vip->std, newstd;
448 int status;
449
Hans Verkuil314527a2013-03-15 06:10:40 -0300450 if (V4L2_STD_ALL == std) {
Laurent Pinchart8774bed2014-04-28 16:53:01 -0300451 v4l2_subdev_call(vip->decoder, video, s_std, std);
Federico Vagaefeb98b2012-04-12 12:39:38 -0300452 ssleep(2);
453 v4l2_subdev_call(vip->decoder, video, querystd, &newstd);
454 v4l2_subdev_call(vip->decoder, video, g_input_status, &status);
455 if (status & V4L2_IN_ST_NO_SIGNAL)
456 return -EIO;
Hans Verkuil314527a2013-03-15 06:10:40 -0300457 std = vip->std = newstd;
458 if (oldstd != std) {
459 if (V4L2_STD_525_60 & std)
Federico Vagaefeb98b2012-04-12 12:39:38 -0300460 vip->format = formats_60[0];
461 else
462 vip->format = formats_50[0];
463 }
464 return 0;
465 }
466
Hans Verkuil314527a2013-03-15 06:10:40 -0300467 if (oldstd != std) {
468 if (V4L2_STD_525_60 & std)
Federico Vagaefeb98b2012-04-12 12:39:38 -0300469 vip->format = formats_60[0];
470 else
471 vip->format = formats_50[0];
472 }
473
Laurent Pinchart8774bed2014-04-28 16:53:01 -0300474 return v4l2_subdev_call(vip->decoder, video, s_std, std);
Federico Vagaefeb98b2012-04-12 12:39:38 -0300475}
476
477/**
478 * vidioc_g_std - get video standard
Federico Vaga8dc97ea2013-02-05 14:34:37 -0300479 * @file: descriptor of device
Federico Vagaefeb98b2012-04-12 12:39:38 -0300480 * @std: contains return values
481 *
482 * the current video standard is returned
483 *
484 * return value: 0, no error.
485 */
486static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *std)
487{
Federico Vaga8dc97ea2013-02-05 14:34:37 -0300488 struct sta2x11_vip *vip = video_drvdata(file);
Federico Vagaefeb98b2012-04-12 12:39:38 -0300489
490 *std = vip->std;
491 return 0;
492}
493
494/**
495 * vidioc_querystd - get possible video standards
Federico Vaga8dc97ea2013-02-05 14:34:37 -0300496 * @file: descriptor of device
Federico Vagaefeb98b2012-04-12 12:39:38 -0300497 * @std: contains return values
498 *
499 * all possible video standards are returned
500 *
501 * return value: delivered by video DAC routine.
502 */
503static int vidioc_querystd(struct file *file, void *priv, v4l2_std_id *std)
504{
Federico Vaga8dc97ea2013-02-05 14:34:37 -0300505 struct sta2x11_vip *vip = video_drvdata(file);
Federico Vagaefeb98b2012-04-12 12:39:38 -0300506
507 return v4l2_subdev_call(vip->decoder, video, querystd, std);
Federico Vagaefeb98b2012-04-12 12:39:38 -0300508}
509
Federico Vagaefeb98b2012-04-12 12:39:38 -0300510static int vidioc_enum_input(struct file *file, void *priv,
511 struct v4l2_input *inp)
512{
513 if (inp->index > 1)
514 return -EINVAL;
515
516 inp->type = V4L2_INPUT_TYPE_CAMERA;
517 inp->std = V4L2_STD_ALL;
518 sprintf(inp->name, "Camera %u", inp->index);
519
520 return 0;
521}
522
523/**
524 * vidioc_s_input - set input line
Federico Vaga8dc97ea2013-02-05 14:34:37 -0300525 * @file: descriptor of device
Federico Vagaefeb98b2012-04-12 12:39:38 -0300526 * @i: new input line number
527 *
528 * the current active input line is set
529 *
530 * return value: 0, no error.
531 *
532 * -EINVAL, line number out of range
533 */
534static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
535{
Federico Vaga8dc97ea2013-02-05 14:34:37 -0300536 struct sta2x11_vip *vip = video_drvdata(file);
Federico Vagaefeb98b2012-04-12 12:39:38 -0300537 int ret;
538
539 if (i > 1)
540 return -EINVAL;
541 ret = v4l2_subdev_call(vip->decoder, video, s_routing, i, 0, 0);
542
543 if (!ret)
544 vip->input = i;
545
546 return 0;
547}
548
549/**
550 * vidioc_g_input - return input line
Federico Vaga8dc97ea2013-02-05 14:34:37 -0300551 * @file: descriptor of device
Federico Vagaefeb98b2012-04-12 12:39:38 -0300552 * @i: returned input line number
553 *
554 * the current active input line is returned
555 *
556 * return value: always 0.
557 */
558static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
559{
Federico Vaga8dc97ea2013-02-05 14:34:37 -0300560 struct sta2x11_vip *vip = video_drvdata(file);
Federico Vagaefeb98b2012-04-12 12:39:38 -0300561
562 *i = vip->input;
563 return 0;
564}
565
566/**
567 * vidioc_enum_fmt_vid_cap - return video capture format
Federico Vagaefeb98b2012-04-12 12:39:38 -0300568 * @f: returned format information
569 *
570 * returns name and format of video capture
571 * Only UYVY is supported by hardware.
572 *
573 * return value: always 0.
574 */
575static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
576 struct v4l2_fmtdesc *f)
577{
578
579 if (f->index != 0)
580 return -EINVAL;
581
582 strcpy(f->description, "4:2:2, packed, UYVY");
583 f->pixelformat = V4L2_PIX_FMT_UYVY;
584 f->flags = 0;
585 return 0;
586}
587
588/**
589 * vidioc_try_fmt_vid_cap - set video capture format
Federico Vaga8dc97ea2013-02-05 14:34:37 -0300590 * @file: descriptor of device
Federico Vagaefeb98b2012-04-12 12:39:38 -0300591 * @f: new format
592 *
593 * new video format is set which includes width and
594 * field type. width is fixed to 720, no scaling.
595 * Only UYVY is supported by this hardware.
596 * the minimum height is 200, the maximum is 576 (PAL)
597 *
598 * return value: 0, no error
599 *
600 * -EINVAL, pixel or field format not supported
601 *
602 */
603static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
604 struct v4l2_format *f)
605{
Federico Vaga8dc97ea2013-02-05 14:34:37 -0300606 struct sta2x11_vip *vip = video_drvdata(file);
Federico Vagaefeb98b2012-04-12 12:39:38 -0300607 int interlace_lim;
608
Federico Vaga8dc97ea2013-02-05 14:34:37 -0300609 if (V4L2_PIX_FMT_UYVY != f->fmt.pix.pixelformat) {
610 v4l2_warn(&vip->v4l2_dev, "Invalid format, only UYVY supported\n");
Federico Vagaefeb98b2012-04-12 12:39:38 -0300611 return -EINVAL;
Federico Vaga8dc97ea2013-02-05 14:34:37 -0300612 }
Federico Vagaefeb98b2012-04-12 12:39:38 -0300613
Mauro Carvalho Chehab6ae009a2012-05-20 12:07:41 -0300614 if (V4L2_STD_525_60 & vip->std)
Federico Vagaefeb98b2012-04-12 12:39:38 -0300615 interlace_lim = 240;
616 else
617 interlace_lim = 288;
618
619 switch (f->fmt.pix.field) {
Federico Vaga8dc97ea2013-02-05 14:34:37 -0300620 default:
Federico Vagaefeb98b2012-04-12 12:39:38 -0300621 case V4L2_FIELD_ANY:
622 if (interlace_lim < f->fmt.pix.height)
623 f->fmt.pix.field = V4L2_FIELD_INTERLACED;
624 else
625 f->fmt.pix.field = V4L2_FIELD_BOTTOM;
626 break;
627 case V4L2_FIELD_TOP:
628 case V4L2_FIELD_BOTTOM:
629 if (interlace_lim < f->fmt.pix.height)
630 f->fmt.pix.height = interlace_lim;
631 break;
632 case V4L2_FIELD_INTERLACED:
633 break;
Federico Vagaefeb98b2012-04-12 12:39:38 -0300634 }
635
Federico Vaga8dc97ea2013-02-05 14:34:37 -0300636 /* It is the only supported format */
637 f->fmt.pix.pixelformat = V4L2_PIX_FMT_UYVY;
Federico Vagaefeb98b2012-04-12 12:39:38 -0300638 f->fmt.pix.height &= ~1;
639 if (2 * interlace_lim < f->fmt.pix.height)
640 f->fmt.pix.height = 2 * interlace_lim;
641 if (200 > f->fmt.pix.height)
642 f->fmt.pix.height = 200;
643 f->fmt.pix.width = 720;
644 f->fmt.pix.bytesperline = f->fmt.pix.width * 2;
645 f->fmt.pix.sizeimage = f->fmt.pix.width * 2 * f->fmt.pix.height;
646 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
Federico Vagaefeb98b2012-04-12 12:39:38 -0300647 return 0;
648}
649
650/**
651 * vidioc_s_fmt_vid_cap - set current video format parameters
Federico Vaga8dc97ea2013-02-05 14:34:37 -0300652 * @file: descriptor of device
Federico Vagaefeb98b2012-04-12 12:39:38 -0300653 * @f: returned format information
654 *
655 * set new capture format
656 * return value: 0, no error
657 *
658 * other, delivered by video DAC routine.
659 */
660static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
661 struct v4l2_format *f)
662{
Federico Vaga8dc97ea2013-02-05 14:34:37 -0300663 struct sta2x11_vip *vip = video_drvdata(file);
664 unsigned int t_stop, b_stop, pitch;
Federico Vagaefeb98b2012-04-12 12:39:38 -0300665 int ret;
666
667 ret = vidioc_try_fmt_vid_cap(file, priv, f);
668 if (ret)
669 return ret;
670
Federico Vaga8dc97ea2013-02-05 14:34:37 -0300671 if (vb2_is_busy(&vip->vb_vidq)) {
672 /* Can't change format during acquisition */
673 v4l2_err(&vip->v4l2_dev, "device busy\n");
674 return -EBUSY;
675 }
676 vip->format = f->fmt.pix;
677 switch (vip->format.field) {
678 case V4L2_FIELD_INTERLACED:
679 t_stop = ((vip->format.height / 2 - 1) << 16) |
680 (2 * vip->format.width - 1);
681 b_stop = t_stop;
682 pitch = 4 * vip->format.width;
683 break;
684 case V4L2_FIELD_TOP:
685 t_stop = ((vip->format.height - 1) << 16) |
686 (2 * vip->format.width - 1);
687 b_stop = (0 << 16) | (2 * vip->format.width - 1);
688 pitch = 2 * vip->format.width;
689 break;
690 case V4L2_FIELD_BOTTOM:
691 t_stop = (0 << 16) | (2 * vip->format.width - 1);
692 b_stop = (vip->format.height << 16) |
693 (2 * vip->format.width - 1);
694 pitch = 2 * vip->format.width;
695 break;
696 default:
697 v4l2_err(&vip->v4l2_dev, "unknown field format\n");
698 return -EINVAL;
699 }
700
701 spin_lock_irq(&vip->slock);
702 /* Y-X Top Field Offset */
703 reg_write(vip, DVP_TFO, 0);
704 /* Y-X Bottom Field Offset */
705 reg_write(vip, DVP_BFO, 0);
706 /* Y-X Top Field Stop*/
707 reg_write(vip, DVP_TFS, t_stop);
708 /* Y-X Bottom Field Stop */
709 reg_write(vip, DVP_BFS, b_stop);
710 /* Video Memory Pitch */
711 reg_write(vip, DVP_VMP, pitch);
712 spin_unlock_irq(&vip->slock);
713
Federico Vagaefeb98b2012-04-12 12:39:38 -0300714 return 0;
715}
716
717/**
718 * vidioc_g_fmt_vid_cap - get current video format parameters
Federico Vaga8dc97ea2013-02-05 14:34:37 -0300719 * @file: descriptor of device
Federico Vagaefeb98b2012-04-12 12:39:38 -0300720 * @f: contains format information
721 *
722 * returns current video format parameters
723 *
724 * return value: 0, always successful
725 */
726static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
727 struct v4l2_format *f)
728{
Federico Vaga8dc97ea2013-02-05 14:34:37 -0300729 struct sta2x11_vip *vip = video_drvdata(file);
Federico Vagaefeb98b2012-04-12 12:39:38 -0300730
Federico Vaga8dc97ea2013-02-05 14:34:37 -0300731 f->fmt.pix = vip->format;
732
Federico Vagaefeb98b2012-04-12 12:39:38 -0300733 return 0;
734}
735
Federico Vagaefeb98b2012-04-12 12:39:38 -0300736static const struct v4l2_ioctl_ops vip_ioctl_ops = {
737 .vidioc_querycap = vidioc_querycap,
Federico Vaga8dc97ea2013-02-05 14:34:37 -0300738 /* FMT handling */
Federico Vagaefeb98b2012-04-12 12:39:38 -0300739 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
Federico Vagaefeb98b2012-04-12 12:39:38 -0300740 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
Federico Vaga8dc97ea2013-02-05 14:34:37 -0300741 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
742 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
743 /* Buffer handlers */
744 .vidioc_create_bufs = vb2_ioctl_create_bufs,
745 .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
746 .vidioc_reqbufs = vb2_ioctl_reqbufs,
747 .vidioc_querybuf = vb2_ioctl_querybuf,
748 .vidioc_qbuf = vb2_ioctl_qbuf,
749 .vidioc_dqbuf = vb2_ioctl_dqbuf,
750 /* Stream on/off */
751 .vidioc_streamon = vb2_ioctl_streamon,
752 .vidioc_streamoff = vb2_ioctl_streamoff,
753 /* Standard handling */
754 .vidioc_g_std = vidioc_g_std,
755 .vidioc_s_std = vidioc_s_std,
756 .vidioc_querystd = vidioc_querystd,
757 /* Input handling */
758 .vidioc_enum_input = vidioc_enum_input,
759 .vidioc_g_input = vidioc_g_input,
760 .vidioc_s_input = vidioc_s_input,
761 /* Log status ioctl */
762 .vidioc_log_status = v4l2_ctrl_log_status,
763 /* Event handling */
764 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
765 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
Federico Vagaefeb98b2012-04-12 12:39:38 -0300766};
767
768static struct video_device video_dev_template = {
Federico Vaga8dc97ea2013-02-05 14:34:37 -0300769 .name = KBUILD_MODNAME,
Hans Verkuil4db4ca742015-03-09 13:34:04 -0300770 .release = video_device_release_empty,
Federico Vagaefeb98b2012-04-12 12:39:38 -0300771 .fops = &vip_fops,
772 .ioctl_ops = &vip_ioctl_ops,
773 .tvnorms = V4L2_STD_ALL,
774};
775
776/**
777 * vip_irq - interrupt routine
778 * @irq: Number of interrupt ( not used, correct number is assumed )
779 * @vip: local data structure containing all information
780 *
781 * check for both frame interrupts set ( top and bottom ).
782 * check FIFO overflow, but limit number of log messages after open.
Federico Vaga8dc97ea2013-02-05 14:34:37 -0300783 * signal a complete buffer if done
Federico Vagaefeb98b2012-04-12 12:39:38 -0300784 *
785 * return value: IRQ_NONE, interrupt was not generated by VIP
786 *
787 * IRQ_HANDLED, interrupt done.
788 */
789static irqreturn_t vip_irq(int irq, struct sta2x11_vip *vip)
790{
Federico Vaga8dc97ea2013-02-05 14:34:37 -0300791 unsigned int status;
Federico Vagaefeb98b2012-04-12 12:39:38 -0300792
Federico Vaga8dc97ea2013-02-05 14:34:37 -0300793 status = reg_read(vip, DVP_ITS);
Federico Vagaefeb98b2012-04-12 12:39:38 -0300794
Federico Vaga8dc97ea2013-02-05 14:34:37 -0300795 if (!status) /* No interrupt to handle */
Federico Vagaefeb98b2012-04-12 12:39:38 -0300796 return IRQ_NONE;
Federico Vagaefeb98b2012-04-12 12:39:38 -0300797
Federico Vaga8dc97ea2013-02-05 14:34:37 -0300798 if (status & DVP_IT_FIFO)
799 if (vip->overflow++ > 5)
800 pr_info("VIP: fifo overflow\n");
Federico Vagaefeb98b2012-04-12 12:39:38 -0300801
Federico Vaga8dc97ea2013-02-05 14:34:37 -0300802 if ((status & DVP_IT_VST) && (status & DVP_IT_VSB)) {
Federico Vagaefeb98b2012-04-12 12:39:38 -0300803 /* this is bad, we are too slow, hope the condition is gone
804 * on the next frame */
Federico Vagaefeb98b2012-04-12 12:39:38 -0300805 return IRQ_HANDLED;
806 }
807
Federico Vaga8dc97ea2013-02-05 14:34:37 -0300808 if (status & DVP_IT_VST)
809 if ((++vip->tcount) < 2)
810 return IRQ_HANDLED;
811 if (status & DVP_IT_VSB) {
812 vip->bcount++;
Federico Vagaefeb98b2012-04-12 12:39:38 -0300813 return IRQ_HANDLED;
Federico Vagaefeb98b2012-04-12 12:39:38 -0300814 }
Federico Vagaefeb98b2012-04-12 12:39:38 -0300815
Federico Vaga8dc97ea2013-02-05 14:34:37 -0300816 if (vip->active) { /* Acquisition is over on this buffer */
817 /* Disable acquisition */
818 reg_write(vip, DVP_CTL, reg_read(vip, DVP_CTL) & ~DVP_CTL_ENA);
819 /* Remove the active buffer from the list */
Junghak Sungd6dd6452015-11-03 08:16:37 -0200820 vip->active->vb.vb2_buf.timestamp = ktime_get_ns();
Junghak Sung2d700712015-09-22 10:30:30 -0300821 vip->active->vb.sequence = vip->sequence++;
822 vb2_buffer_done(&vip->active->vb.vb2_buf, VB2_BUF_STATE_DONE);
Federico Vagaefeb98b2012-04-12 12:39:38 -0300823 }
Federico Vaga8dc97ea2013-02-05 14:34:37 -0300824
Federico Vagaefeb98b2012-04-12 12:39:38 -0300825 return IRQ_HANDLED;
826}
827
Federico Vaga8dc97ea2013-02-05 14:34:37 -0300828static void sta2x11_vip_init_register(struct sta2x11_vip *vip)
829{
830 /* Register initialization */
831 spin_lock_irq(&vip->slock);
832 /* Clean interrupt */
833 reg_read(vip, DVP_ITS);
834 /* Enable Half Line per vertical */
835 reg_write(vip, DVP_HLFLN, DVP_HLFLN_SD);
836 /* Reset VIP control */
837 reg_write(vip, DVP_CTL, DVP_CTL_RST);
838 /* Clear VIP control */
839 reg_write(vip, DVP_CTL, 0);
840 spin_unlock_irq(&vip->slock);
841}
842static void sta2x11_vip_clear_register(struct sta2x11_vip *vip)
843{
844 spin_lock_irq(&vip->slock);
845 /* Disable interrupt */
846 reg_write(vip, DVP_ITM, 0);
847 /* Reset VIP Control */
848 reg_write(vip, DVP_CTL, DVP_CTL_RST);
849 /* Clear VIP Control */
850 reg_write(vip, DVP_CTL, 0);
851 /* Clean VIP Interrupt */
852 reg_read(vip, DVP_ITS);
853 spin_unlock_irq(&vip->slock);
854}
855static int sta2x11_vip_init_buffer(struct sta2x11_vip *vip)
856{
857 int err;
858
859 err = dma_set_coherent_mask(&vip->pdev->dev, DMA_BIT_MASK(29));
860 if (err) {
861 v4l2_err(&vip->v4l2_dev, "Cannot configure coherent mask");
862 return err;
863 }
864 memset(&vip->vb_vidq, 0, sizeof(struct vb2_queue));
865 vip->vb_vidq.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
866 vip->vb_vidq.io_modes = VB2_MMAP | VB2_READ;
867 vip->vb_vidq.drv_priv = vip;
868 vip->vb_vidq.buf_struct_size = sizeof(struct vip_buffer);
869 vip->vb_vidq.ops = &vip_video_qops;
870 vip->vb_vidq.mem_ops = &vb2_dma_contig_memops;
Hans Verkuilb59b1002015-05-07 03:22:14 -0300871 vip->vb_vidq.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
Federico Vaga8dc97ea2013-02-05 14:34:37 -0300872 err = vb2_queue_init(&vip->vb_vidq);
873 if (err)
874 return err;
875 INIT_LIST_HEAD(&vip->buffer_list);
876 spin_lock_init(&vip->lock);
877
878
879 vip->alloc_ctx = vb2_dma_contig_init_ctx(&vip->pdev->dev);
880 if (IS_ERR(vip->alloc_ctx)) {
881 v4l2_err(&vip->v4l2_dev, "Can't allocate buffer context");
882 return PTR_ERR(vip->alloc_ctx);
883 }
884
885 return 0;
886}
887static void sta2x11_vip_release_buffer(struct sta2x11_vip *vip)
888{
889 vb2_dma_contig_cleanup_ctx(vip->alloc_ctx);
890}
891static int sta2x11_vip_init_controls(struct sta2x11_vip *vip)
892{
893 /*
894 * Inititialize an empty control so VIP can inerithing controls
895 * from ADV7180
896 */
897 v4l2_ctrl_handler_init(&vip->ctrl_hdl, 0);
898
899 vip->v4l2_dev.ctrl_handler = &vip->ctrl_hdl;
900 if (vip->ctrl_hdl.error) {
901 int err = vip->ctrl_hdl.error;
902
903 v4l2_ctrl_handler_free(&vip->ctrl_hdl);
904 return err;
905 }
906
907 return 0;
908}
909
Federico Vagaefeb98b2012-04-12 12:39:38 -0300910/**
911 * vip_gpio_reserve - reserve gpio pin
912 * @dev: device
913 * @pin: GPIO pin number
914 * @dir: direction, input or output
915 * @name: GPIO pin name
916 *
917 */
918static int vip_gpio_reserve(struct device *dev, int pin, int dir,
919 const char *name)
920{
921 int ret;
922
923 if (pin == -1)
924 return 0;
925
926 ret = gpio_request(pin, name);
927 if (ret) {
928 dev_err(dev, "Failed to allocate pin %d (%s)\n", pin, name);
929 return ret;
930 }
931
932 ret = gpio_direction_output(pin, dir);
933 if (ret) {
934 dev_err(dev, "Failed to set direction for pin %d (%s)\n",
935 pin, name);
936 gpio_free(pin);
937 return ret;
938 }
939
940 ret = gpio_export(pin, false);
941 if (ret) {
942 dev_err(dev, "Failed to export pin %d (%s)\n", pin, name);
943 gpio_free(pin);
944 return ret;
945 }
946
947 return 0;
948}
949
950/**
951 * vip_gpio_release - release gpio pin
952 * @dev: device
953 * @pin: GPIO pin number
954 * @name: GPIO pin name
955 *
956 */
957static void vip_gpio_release(struct device *dev, int pin, const char *name)
958{
959 if (pin != -1) {
960 dev_dbg(dev, "releasing pin %d (%s)\n", pin, name);
961 gpio_unexport(pin);
962 gpio_free(pin);
963 }
964}
965
966/**
967 * sta2x11_vip_init_one - init one instance of video device
968 * @pdev: PCI device
969 * @ent: (not used)
970 *
971 * allocate reset pins for DAC.
972 * Reset video DAC, this is done via reset line.
973 * allocate memory for managing device
974 * request interrupt
975 * map IO region
976 * register device
977 * find and initialize video DAC
978 *
979 * return value: 0, no error
980 *
981 * -ENOMEM, no memory
982 *
983 * -ENODEV, device could not be detected or registered
984 */
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -0800985static int sta2x11_vip_init_one(struct pci_dev *pdev,
986 const struct pci_device_id *ent)
Federico Vagaefeb98b2012-04-12 12:39:38 -0300987{
988 int ret;
989 struct sta2x11_vip *vip;
990 struct vip_config *config;
991
Federico Vaga8dc97ea2013-02-05 14:34:37 -0300992 /* Check if hardware support 26-bit DMA */
993 if (dma_set_mask(&pdev->dev, DMA_BIT_MASK(26))) {
994 dev_err(&pdev->dev, "26-bit DMA addressing not available\n");
995 return -EINVAL;
996 }
997 /* Enable PCI */
Federico Vagaefeb98b2012-04-12 12:39:38 -0300998 ret = pci_enable_device(pdev);
999 if (ret)
1000 return ret;
1001
Federico Vaga8dc97ea2013-02-05 14:34:37 -03001002 /* Get VIP platform data */
Federico Vagaefeb98b2012-04-12 12:39:38 -03001003 config = dev_get_platdata(&pdev->dev);
1004 if (!config) {
1005 dev_info(&pdev->dev, "VIP slot disabled\n");
1006 ret = -EINVAL;
1007 goto disable;
1008 }
1009
Federico Vaga8dc97ea2013-02-05 14:34:37 -03001010 /* Power configuration */
Federico Vagaefeb98b2012-04-12 12:39:38 -03001011 ret = vip_gpio_reserve(&pdev->dev, config->pwr_pin, 0,
1012 config->pwr_name);
1013 if (ret)
1014 goto disable;
1015
1016 if (config->reset_pin >= 0) {
1017 ret = vip_gpio_reserve(&pdev->dev, config->reset_pin, 0,
1018 config->reset_name);
1019 if (ret) {
1020 vip_gpio_release(&pdev->dev, config->pwr_pin,
1021 config->pwr_name);
1022 goto disable;
1023 }
1024 }
Federico Vagaefeb98b2012-04-12 12:39:38 -03001025 if (config->pwr_pin != -1) {
1026 /* Datasheet says 5ms between PWR and RST */
1027 usleep_range(5000, 25000);
1028 ret = gpio_direction_output(config->pwr_pin, 1);
1029 }
1030
1031 if (config->reset_pin != -1) {
1032 /* Datasheet says 5ms between PWR and RST */
1033 usleep_range(5000, 25000);
1034 ret = gpio_direction_output(config->reset_pin, 1);
1035 }
1036 usleep_range(5000, 25000);
1037
Federico Vaga8dc97ea2013-02-05 14:34:37 -03001038 /* Allocate a new VIP instance */
Federico Vagaefeb98b2012-04-12 12:39:38 -03001039 vip = kzalloc(sizeof(struct sta2x11_vip), GFP_KERNEL);
1040 if (!vip) {
1041 ret = -ENOMEM;
1042 goto release_gpios;
1043 }
Federico Vagaefeb98b2012-04-12 12:39:38 -03001044 vip->pdev = pdev;
1045 vip->std = V4L2_STD_PAL;
1046 vip->format = formats_50[0];
1047 vip->config = config;
1048
Federico Vaga8dc97ea2013-02-05 14:34:37 -03001049 ret = sta2x11_vip_init_controls(vip);
1050 if (ret)
1051 goto free_mem;
Wei Yongjund0176502013-05-13 10:00:01 -03001052 ret = v4l2_device_register(&pdev->dev, &vip->v4l2_dev);
1053 if (ret)
Federico Vagaefeb98b2012-04-12 12:39:38 -03001054 goto free_mem;
1055
1056 dev_dbg(&pdev->dev, "BAR #0 at 0x%lx 0x%lx irq %d\n",
1057 (unsigned long)pci_resource_start(pdev, 0),
1058 (unsigned long)pci_resource_len(pdev, 0), pdev->irq);
1059
1060 pci_set_master(pdev);
1061
Federico Vaga8dc97ea2013-02-05 14:34:37 -03001062 ret = pci_request_regions(pdev, KBUILD_MODNAME);
Federico Vagaefeb98b2012-04-12 12:39:38 -03001063 if (ret)
1064 goto unreg;
1065
1066 vip->iomem = pci_iomap(pdev, 0, 0x100);
1067 if (!vip->iomem) {
Federico Vaga8dc97ea2013-02-05 14:34:37 -03001068 ret = -ENOMEM;
Federico Vagaefeb98b2012-04-12 12:39:38 -03001069 goto release;
1070 }
1071
1072 pci_enable_msi(pdev);
1073
Federico Vaga8dc97ea2013-02-05 14:34:37 -03001074 /* Initialize buffer */
1075 ret = sta2x11_vip_init_buffer(vip);
1076 if (ret)
1077 goto unmap;
1078
Federico Vagaefeb98b2012-04-12 12:39:38 -03001079 spin_lock_init(&vip->slock);
Federico Vagaefeb98b2012-04-12 12:39:38 -03001080
1081 ret = request_irq(pdev->irq,
1082 (irq_handler_t) vip_irq,
Federico Vaga8dc97ea2013-02-05 14:34:37 -03001083 IRQF_SHARED, KBUILD_MODNAME, vip);
Federico Vagaefeb98b2012-04-12 12:39:38 -03001084 if (ret) {
1085 dev_err(&pdev->dev, "request_irq failed\n");
1086 ret = -ENODEV;
Federico Vaga8dc97ea2013-02-05 14:34:37 -03001087 goto release_buf;
Federico Vagaefeb98b2012-04-12 12:39:38 -03001088 }
1089
Hans Verkuil4db4ca742015-03-09 13:34:04 -03001090 /* Initialize and register video device */
1091 vip->video_dev = video_dev_template;
1092 vip->video_dev.v4l2_dev = &vip->v4l2_dev;
1093 vip->video_dev.queue = &vip->vb_vidq;
1094 video_set_drvdata(&vip->video_dev, vip);
Federico Vagaefeb98b2012-04-12 12:39:38 -03001095
Hans Verkuil4db4ca742015-03-09 13:34:04 -03001096 ret = video_register_device(&vip->video_dev, VFL_TYPE_GRABBER, -1);
Federico Vagaefeb98b2012-04-12 12:39:38 -03001097 if (ret)
1098 goto vrelease;
1099
Federico Vaga8dc97ea2013-02-05 14:34:37 -03001100 /* Get ADV7180 subdevice */
Federico Vagaefeb98b2012-04-12 12:39:38 -03001101 vip->adapter = i2c_get_adapter(vip->config->i2c_id);
1102 if (!vip->adapter) {
1103 ret = -ENODEV;
1104 dev_err(&pdev->dev, "no I2C adapter found\n");
1105 goto vunreg;
1106 }
1107
1108 vip->decoder = v4l2_i2c_new_subdev(&vip->v4l2_dev, vip->adapter,
1109 "adv7180", vip->config->i2c_addr,
1110 NULL);
1111 if (!vip->decoder) {
1112 ret = -ENODEV;
1113 dev_err(&pdev->dev, "no decoder found\n");
1114 goto vunreg;
1115 }
1116
1117 i2c_put_adapter(vip->adapter);
Federico Vagaefeb98b2012-04-12 12:39:38 -03001118 v4l2_subdev_call(vip->decoder, core, init, 0);
1119
Federico Vaga8dc97ea2013-02-05 14:34:37 -03001120 sta2x11_vip_init_register(vip);
1121
1122 dev_info(&pdev->dev, "STA2X11 Video Input Port (VIP) loaded\n");
Federico Vagaefeb98b2012-04-12 12:39:38 -03001123 return 0;
1124
1125vunreg:
Hans Verkuil4db4ca742015-03-09 13:34:04 -03001126 video_set_drvdata(&vip->video_dev, NULL);
Federico Vagaefeb98b2012-04-12 12:39:38 -03001127vrelease:
Hans Verkuil4db4ca742015-03-09 13:34:04 -03001128 video_unregister_device(&vip->video_dev);
Federico Vagaefeb98b2012-04-12 12:39:38 -03001129 free_irq(pdev->irq, vip);
Federico Vaga8dc97ea2013-02-05 14:34:37 -03001130release_buf:
1131 sta2x11_vip_release_buffer(vip);
Federico Vagaefeb98b2012-04-12 12:39:38 -03001132 pci_disable_msi(pdev);
1133unmap:
Federico Vaga8dc97ea2013-02-05 14:34:37 -03001134 vb2_queue_release(&vip->vb_vidq);
Federico Vagaefeb98b2012-04-12 12:39:38 -03001135 pci_iounmap(pdev, vip->iomem);
Federico Vagaefeb98b2012-04-12 12:39:38 -03001136release:
1137 pci_release_regions(pdev);
1138unreg:
1139 v4l2_device_unregister(&vip->v4l2_dev);
1140free_mem:
1141 kfree(vip);
1142release_gpios:
1143 vip_gpio_release(&pdev->dev, config->reset_pin, config->reset_name);
1144 vip_gpio_release(&pdev->dev, config->pwr_pin, config->pwr_name);
1145disable:
1146 /*
1147 * do not call pci_disable_device on sta2x11 because it break all
1148 * other Bus masters on this EP
1149 */
1150 return ret;
1151}
1152
1153/**
1154 * sta2x11_vip_remove_one - release device
1155 * @pdev: PCI device
1156 *
1157 * Undo everything done in .._init_one
1158 *
1159 * unregister video device
1160 * free interrupt
1161 * unmap ioadresses
1162 * free memory
1163 * free GPIO pins
1164 */
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08001165static void sta2x11_vip_remove_one(struct pci_dev *pdev)
Federico Vagaefeb98b2012-04-12 12:39:38 -03001166{
1167 struct v4l2_device *v4l2_dev = pci_get_drvdata(pdev);
1168 struct sta2x11_vip *vip =
1169 container_of(v4l2_dev, struct sta2x11_vip, v4l2_dev);
1170
Federico Vaga8dc97ea2013-02-05 14:34:37 -03001171 sta2x11_vip_clear_register(vip);
1172
Hans Verkuil4db4ca742015-03-09 13:34:04 -03001173 video_set_drvdata(&vip->video_dev, NULL);
1174 video_unregister_device(&vip->video_dev);
Federico Vagaefeb98b2012-04-12 12:39:38 -03001175 free_irq(pdev->irq, vip);
1176 pci_disable_msi(pdev);
Federico Vaga8dc97ea2013-02-05 14:34:37 -03001177 vb2_queue_release(&vip->vb_vidq);
Federico Vagaefeb98b2012-04-12 12:39:38 -03001178 pci_iounmap(pdev, vip->iomem);
1179 pci_release_regions(pdev);
1180
1181 v4l2_device_unregister(&vip->v4l2_dev);
Federico Vagaefeb98b2012-04-12 12:39:38 -03001182
1183 vip_gpio_release(&pdev->dev, vip->config->pwr_pin,
1184 vip->config->pwr_name);
1185 vip_gpio_release(&pdev->dev, vip->config->reset_pin,
1186 vip->config->reset_name);
1187
1188 kfree(vip);
1189 /*
1190 * do not call pci_disable_device on sta2x11 because it break all
1191 * other Bus masters on this EP
1192 */
1193}
1194
1195#ifdef CONFIG_PM
1196
1197/**
1198 * sta2x11_vip_suspend - set device into power save mode
1199 * @pdev: PCI device
1200 * @state: new state of device
1201 *
1202 * all relevant registers are saved and an attempt to set a new state is made.
1203 *
1204 * return value: 0 always indicate success,
1205 * even if device could not be disabled. (workaround for hardware problem)
Federico Vagaefeb98b2012-04-12 12:39:38 -03001206 */
1207static int sta2x11_vip_suspend(struct pci_dev *pdev, pm_message_t state)
1208{
1209 struct v4l2_device *v4l2_dev = pci_get_drvdata(pdev);
1210 struct sta2x11_vip *vip =
1211 container_of(v4l2_dev, struct sta2x11_vip, v4l2_dev);
1212 unsigned long flags;
1213 int i;
1214
1215 spin_lock_irqsave(&vip->slock, flags);
Federico Vaga8dc97ea2013-02-05 14:34:37 -03001216 vip->register_save_area[0] = reg_read(vip, DVP_CTL);
1217 reg_write(vip, DVP_CTL, vip->register_save_area[0] & DVP_CTL_DIS);
1218 vip->register_save_area[SAVE_COUNT] = reg_read(vip, DVP_ITM);
1219 reg_write(vip, DVP_ITM, 0);
Federico Vagaefeb98b2012-04-12 12:39:38 -03001220 for (i = 1; i < SAVE_COUNT; i++)
Federico Vaga8dc97ea2013-02-05 14:34:37 -03001221 vip->register_save_area[i] = reg_read(vip, 4 * i);
Federico Vagaefeb98b2012-04-12 12:39:38 -03001222 for (i = 0; i < AUX_COUNT; i++)
1223 vip->register_save_area[SAVE_COUNT + IRQ_COUNT + i] =
Federico Vaga8dc97ea2013-02-05 14:34:37 -03001224 reg_read(vip, registers_to_save[i]);
Federico Vagaefeb98b2012-04-12 12:39:38 -03001225 spin_unlock_irqrestore(&vip->slock, flags);
1226 /* save pci state */
1227 pci_save_state(pdev);
1228 if (pci_set_power_state(pdev, pci_choose_state(pdev, state))) {
1229 /*
1230 * do not call pci_disable_device on sta2x11 because it
1231 * break all other Bus masters on this EP
1232 */
1233 vip->disabled = 1;
1234 }
1235
1236 pr_info("VIP: suspend\n");
1237 return 0;
1238}
1239
1240/**
1241 * sta2x11_vip_resume - resume device operation
1242 * @pdev : PCI device
1243 *
1244 * re-enable device, set PCI state to powered and restore registers.
1245 * resume normal device operation afterwards.
1246 *
1247 * return value: 0, no error.
1248 *
1249 * other, could not set device to power on state.
1250 */
1251static int sta2x11_vip_resume(struct pci_dev *pdev)
1252{
1253 struct v4l2_device *v4l2_dev = pci_get_drvdata(pdev);
1254 struct sta2x11_vip *vip =
1255 container_of(v4l2_dev, struct sta2x11_vip, v4l2_dev);
1256 unsigned long flags;
1257 int ret, i;
1258
1259 pr_info("VIP: resume\n");
1260 /* restore pci state */
1261 if (vip->disabled) {
1262 ret = pci_enable_device(pdev);
1263 if (ret) {
Federico Vaga8dc97ea2013-02-05 14:34:37 -03001264 pr_warn("VIP: Can't enable device.\n");
Federico Vagaefeb98b2012-04-12 12:39:38 -03001265 return ret;
1266 }
1267 vip->disabled = 0;
1268 }
1269 ret = pci_set_power_state(pdev, PCI_D0);
1270 if (ret) {
1271 /*
1272 * do not call pci_disable_device on sta2x11 because it
1273 * break all other Bus masters on this EP
1274 */
Federico Vaga8dc97ea2013-02-05 14:34:37 -03001275 pr_warn("VIP: Can't enable device.\n");
Federico Vagaefeb98b2012-04-12 12:39:38 -03001276 vip->disabled = 1;
1277 return ret;
1278 }
1279
1280 pci_restore_state(pdev);
1281
1282 spin_lock_irqsave(&vip->slock, flags);
1283 for (i = 1; i < SAVE_COUNT; i++)
Federico Vaga8dc97ea2013-02-05 14:34:37 -03001284 reg_write(vip, 4 * i, vip->register_save_area[i]);
Federico Vagaefeb98b2012-04-12 12:39:38 -03001285 for (i = 0; i < AUX_COUNT; i++)
Federico Vaga8dc97ea2013-02-05 14:34:37 -03001286 reg_write(vip, registers_to_save[i],
Federico Vagaefeb98b2012-04-12 12:39:38 -03001287 vip->register_save_area[SAVE_COUNT + IRQ_COUNT + i]);
Federico Vaga8dc97ea2013-02-05 14:34:37 -03001288 reg_write(vip, DVP_CTL, vip->register_save_area[0]);
1289 reg_write(vip, DVP_ITM, vip->register_save_area[SAVE_COUNT]);
Federico Vagaefeb98b2012-04-12 12:39:38 -03001290 spin_unlock_irqrestore(&vip->slock, flags);
1291 return 0;
1292}
1293
1294#endif
1295
Jingoo Hanf1b84d32013-12-02 20:15:04 -03001296static const struct pci_device_id sta2x11_vip_pci_tbl[] = {
Federico Vagaefeb98b2012-04-12 12:39:38 -03001297 {PCI_DEVICE(PCI_VENDOR_ID_STMICRO, PCI_DEVICE_ID_STMICRO_VIP)},
1298 {0,}
1299};
1300
1301static struct pci_driver sta2x11_vip_driver = {
Federico Vaga8dc97ea2013-02-05 14:34:37 -03001302 .name = KBUILD_MODNAME,
Federico Vagaefeb98b2012-04-12 12:39:38 -03001303 .probe = sta2x11_vip_init_one,
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08001304 .remove = sta2x11_vip_remove_one,
Federico Vagaefeb98b2012-04-12 12:39:38 -03001305 .id_table = sta2x11_vip_pci_tbl,
1306#ifdef CONFIG_PM
1307 .suspend = sta2x11_vip_suspend,
1308 .resume = sta2x11_vip_resume,
1309#endif
1310};
1311
1312static int __init sta2x11_vip_init_module(void)
1313{
1314 return pci_register_driver(&sta2x11_vip_driver);
1315}
1316
1317static void __exit sta2x11_vip_exit_module(void)
1318{
1319 pci_unregister_driver(&sta2x11_vip_driver);
1320}
1321
1322#ifdef MODULE
1323module_init(sta2x11_vip_init_module);
1324module_exit(sta2x11_vip_exit_module);
1325#else
1326late_initcall_sync(sta2x11_vip_init_module);
1327#endif
1328
1329MODULE_DESCRIPTION("STA2X11 Video Input Port driver");
1330MODULE_AUTHOR("Wind River");
1331MODULE_LICENSE("GPL v2");
1332MODULE_SUPPORTED_DEVICE("sta2x11 video input");
1333MODULE_VERSION(DRV_VERSION);
1334MODULE_DEVICE_TABLE(pci, sta2x11_vip_pci_tbl);