blob: 77edc113e48520228b587fb2d092e94f0ccfb54a [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 {
91 struct vb2_buffer vb;
92 struct list_head list;
93 dma_addr_t dma;
94};
95static inline struct vip_buffer *to_vip_buffer(struct vb2_buffer *vb2)
96{
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;
130 struct video_device *video_dev;
131 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
155 void *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 */
268static int queue_setup(struct vb2_queue *vq, const struct v4l2_format *fmt,
269 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{
290 struct vip_buffer *vip_buf = to_vip_buffer(vb);
Federico Vagaefeb98b2012-04-12 12:39:38 -0300291
Federico Vaga8dc97ea2013-02-05 14:34:37 -0300292 vip_buf->dma = vb2_dma_contig_plane_dma_addr(vb, 0);
293 INIT_LIST_HEAD(&vip_buf->list);
Federico Vagaefeb98b2012-04-12 12:39:38 -0300294 return 0;
295}
296
Federico Vaga8dc97ea2013-02-05 14:34:37 -0300297static int buffer_prepare(struct vb2_buffer *vb)
Federico Vagaefeb98b2012-04-12 12:39:38 -0300298{
Federico Vaga8dc97ea2013-02-05 14:34:37 -0300299 struct sta2x11_vip *vip = vb2_get_drv_priv(vb->vb2_queue);
300 struct vip_buffer *vip_buf = to_vip_buffer(vb);
301 unsigned long size;
Federico Vagaefeb98b2012-04-12 12:39:38 -0300302
Federico Vaga8dc97ea2013-02-05 14:34:37 -0300303 size = vip->format.sizeimage;
304 if (vb2_plane_size(vb, 0) < size) {
305 v4l2_err(&vip->v4l2_dev, "buffer too small (%lu < %lu)\n",
306 vb2_plane_size(vb, 0), size);
307 return -EINVAL;
308 }
309
310 vb2_set_plane_payload(&vip_buf->vb, 0, size);
311
312 return 0;
313}
314static void buffer_queue(struct vb2_buffer *vb)
315{
316 struct sta2x11_vip *vip = vb2_get_drv_priv(vb->vb2_queue);
317 struct vip_buffer *vip_buf = to_vip_buffer(vb);
318
319 spin_lock(&vip->lock);
320 list_add_tail(&vip_buf->list, &vip->buffer_list);
321 if (!vip->active) { /* No active buffer, active the first one */
322 vip->active = list_first_entry(&vip->buffer_list,
323 struct vip_buffer,
324 list);
325 if (vb2_is_streaming(&vip->vb_vidq)) /* streaming is on */
326 start_dma(vip, vip_buf); /* start dma capture */
327 }
328 spin_unlock(&vip->lock);
329}
330static int buffer_finish(struct vb2_buffer *vb)
331{
332 struct sta2x11_vip *vip = vb2_get_drv_priv(vb->vb2_queue);
333 struct vip_buffer *vip_buf = to_vip_buffer(vb);
334
335 /* Buffer handled, remove it from the list */
336 spin_lock(&vip->lock);
337 list_del_init(&vip_buf->list);
338 spin_unlock(&vip->lock);
339
340 vip_active_buf_next(vip);
341
342 return 0;
343}
344
345static int start_streaming(struct vb2_queue *vq, unsigned int count)
346{
347 struct sta2x11_vip *vip = vb2_get_drv_priv(vq);
348
Federico Vagaefeb98b2012-04-12 12:39:38 -0300349 spin_lock_irq(&vip->slock);
Federico Vaga8dc97ea2013-02-05 14:34:37 -0300350 /* Enable interrupt VSYNC Top and Bottom*/
351 reg_write(vip, DVP_ITM, DVP_IT_VSB | DVP_IT_VST);
Federico Vagaefeb98b2012-04-12 12:39:38 -0300352 spin_unlock_irq(&vip->slock);
353
Federico Vaga8dc97ea2013-02-05 14:34:37 -0300354 if (count)
355 start_dma(vip, vip->active);
Federico Vagaefeb98b2012-04-12 12:39:38 -0300356
Federico Vagaefeb98b2012-04-12 12:39:38 -0300357 return 0;
358}
359
Federico Vaga8dc97ea2013-02-05 14:34:37 -0300360/* abort streaming and wait for last buffer */
361static int stop_streaming(struct vb2_queue *vq)
Federico Vagaefeb98b2012-04-12 12:39:38 -0300362{
Federico Vaga8dc97ea2013-02-05 14:34:37 -0300363 struct sta2x11_vip *vip = vb2_get_drv_priv(vq);
364 struct vip_buffer *vip_buf, *node;
Federico Vagaefeb98b2012-04-12 12:39:38 -0300365
Federico Vaga8dc97ea2013-02-05 14:34:37 -0300366 /* Disable acquisition */
367 reg_write(vip, DVP_CTL, reg_read(vip, DVP_CTL) & ~DVP_CTL_ENA);
368 /* Disable all interrupts */
369 reg_write(vip, DVP_ITM, 0);
370
371 /* Release all active buffers */
372 spin_lock(&vip->lock);
373 list_for_each_entry_safe(vip_buf, node, &vip->buffer_list, list) {
374 vb2_buffer_done(&vip_buf->vb, VB2_BUF_STATE_ERROR);
375 list_del(&vip_buf->list);
376 }
377 spin_unlock(&vip->lock);
378 return 0;
Federico Vagaefeb98b2012-04-12 12:39:38 -0300379}
380
Federico Vaga8dc97ea2013-02-05 14:34:37 -0300381static struct vb2_ops vip_video_qops = {
382 .queue_setup = queue_setup,
383 .buf_init = buffer_init,
384 .buf_prepare = buffer_prepare,
385 .buf_finish = buffer_finish,
386 .buf_queue = buffer_queue,
387 .start_streaming = start_streaming,
388 .stop_streaming = stop_streaming,
389};
Federico Vagaefeb98b2012-04-12 12:39:38 -0300390
Federico Vagaefeb98b2012-04-12 12:39:38 -0300391
Federico Vaga8dc97ea2013-02-05 14:34:37 -0300392/* File Operations */
393static const struct v4l2_file_operations vip_fops = {
394 .owner = THIS_MODULE,
395 .open = v4l2_fh_open,
396 .release = vb2_fop_release,
397 .unlocked_ioctl = video_ioctl2,
398 .read = vb2_fop_read,
399 .mmap = vb2_fop_mmap,
400 .poll = vb2_fop_poll
401};
Federico Vagaefeb98b2012-04-12 12:39:38 -0300402
Federico Vagaefeb98b2012-04-12 12:39:38 -0300403
404/**
405 * vidioc_querycap - return capabilities of device
Federico Vaga8dc97ea2013-02-05 14:34:37 -0300406 * @file: descriptor of device
Federico Vagaefeb98b2012-04-12 12:39:38 -0300407 * @cap: contains return values
408 *
409 * the capabilities of the device are returned
410 *
411 * return value: 0, no error.
412 */
413static int vidioc_querycap(struct file *file, void *priv,
414 struct v4l2_capability *cap)
415{
Federico Vaga8dc97ea2013-02-05 14:34:37 -0300416 struct sta2x11_vip *vip = video_drvdata(file);
Federico Vagaefeb98b2012-04-12 12:39:38 -0300417
Federico Vaga8dc97ea2013-02-05 14:34:37 -0300418 strcpy(cap->driver, KBUILD_MODNAME);
419 strcpy(cap->card, KBUILD_MODNAME);
Federico Vagaefeb98b2012-04-12 12:39:38 -0300420 snprintf(cap->bus_info, sizeof(cap->bus_info), "PCI:%s",
421 pci_name(vip->pdev));
Federico Vaga8dc97ea2013-02-05 14:34:37 -0300422 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE |
423 V4L2_CAP_STREAMING;
424 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
Federico Vagaefeb98b2012-04-12 12:39:38 -0300425
426 return 0;
427}
428
429/**
430 * vidioc_s_std - set video standard
Federico Vaga8dc97ea2013-02-05 14:34:37 -0300431 * @file: descriptor of device
Federico Vagaefeb98b2012-04-12 12:39:38 -0300432 * @std: contains standard to be set
433 *
434 * the video standard is set
435 *
436 * return value: 0, no error.
437 *
438 * -EIO, no input signal detected
439 *
440 * other, returned from video DAC.
441 */
Hans Verkuil314527a2013-03-15 06:10:40 -0300442static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id std)
Federico Vagaefeb98b2012-04-12 12:39:38 -0300443{
Federico Vaga8dc97ea2013-02-05 14:34:37 -0300444 struct sta2x11_vip *vip = video_drvdata(file);
Federico Vagaefeb98b2012-04-12 12:39:38 -0300445 v4l2_std_id oldstd = vip->std, newstd;
446 int status;
447
Hans Verkuil314527a2013-03-15 06:10:40 -0300448 if (V4L2_STD_ALL == std) {
449 v4l2_subdev_call(vip->decoder, core, s_std, std);
Federico Vagaefeb98b2012-04-12 12:39:38 -0300450 ssleep(2);
451 v4l2_subdev_call(vip->decoder, video, querystd, &newstd);
452 v4l2_subdev_call(vip->decoder, video, g_input_status, &status);
453 if (status & V4L2_IN_ST_NO_SIGNAL)
454 return -EIO;
Hans Verkuil314527a2013-03-15 06:10:40 -0300455 std = vip->std = newstd;
456 if (oldstd != std) {
457 if (V4L2_STD_525_60 & std)
Federico Vagaefeb98b2012-04-12 12:39:38 -0300458 vip->format = formats_60[0];
459 else
460 vip->format = formats_50[0];
461 }
462 return 0;
463 }
464
Hans Verkuil314527a2013-03-15 06:10:40 -0300465 if (oldstd != std) {
466 if (V4L2_STD_525_60 & std)
Federico Vagaefeb98b2012-04-12 12:39:38 -0300467 vip->format = formats_60[0];
468 else
469 vip->format = formats_50[0];
470 }
471
Hans Verkuil314527a2013-03-15 06:10:40 -0300472 return v4l2_subdev_call(vip->decoder, core, s_std, std);
Federico Vagaefeb98b2012-04-12 12:39:38 -0300473}
474
475/**
476 * vidioc_g_std - get video standard
Federico Vaga8dc97ea2013-02-05 14:34:37 -0300477 * @file: descriptor of device
Federico Vagaefeb98b2012-04-12 12:39:38 -0300478 * @std: contains return values
479 *
480 * the current video standard is returned
481 *
482 * return value: 0, no error.
483 */
484static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *std)
485{
Federico Vaga8dc97ea2013-02-05 14:34:37 -0300486 struct sta2x11_vip *vip = video_drvdata(file);
Federico Vagaefeb98b2012-04-12 12:39:38 -0300487
488 *std = vip->std;
489 return 0;
490}
491
492/**
493 * vidioc_querystd - get possible video standards
Federico Vaga8dc97ea2013-02-05 14:34:37 -0300494 * @file: descriptor of device
Federico Vagaefeb98b2012-04-12 12:39:38 -0300495 * @std: contains return values
496 *
497 * all possible video standards are returned
498 *
499 * return value: delivered by video DAC routine.
500 */
501static int vidioc_querystd(struct file *file, void *priv, v4l2_std_id *std)
502{
Federico Vaga8dc97ea2013-02-05 14:34:37 -0300503 struct sta2x11_vip *vip = video_drvdata(file);
Federico Vagaefeb98b2012-04-12 12:39:38 -0300504
505 return v4l2_subdev_call(vip->decoder, video, querystd, std);
Federico Vagaefeb98b2012-04-12 12:39:38 -0300506}
507
Federico Vagaefeb98b2012-04-12 12:39:38 -0300508static int vidioc_enum_input(struct file *file, void *priv,
509 struct v4l2_input *inp)
510{
511 if (inp->index > 1)
512 return -EINVAL;
513
514 inp->type = V4L2_INPUT_TYPE_CAMERA;
515 inp->std = V4L2_STD_ALL;
516 sprintf(inp->name, "Camera %u", inp->index);
517
518 return 0;
519}
520
521/**
522 * vidioc_s_input - set input line
Federico Vaga8dc97ea2013-02-05 14:34:37 -0300523 * @file: descriptor of device
Federico Vagaefeb98b2012-04-12 12:39:38 -0300524 * @i: new input line number
525 *
526 * the current active input line is set
527 *
528 * return value: 0, no error.
529 *
530 * -EINVAL, line number out of range
531 */
532static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
533{
Federico Vaga8dc97ea2013-02-05 14:34:37 -0300534 struct sta2x11_vip *vip = video_drvdata(file);
Federico Vagaefeb98b2012-04-12 12:39:38 -0300535 int ret;
536
537 if (i > 1)
538 return -EINVAL;
539 ret = v4l2_subdev_call(vip->decoder, video, s_routing, i, 0, 0);
540
541 if (!ret)
542 vip->input = i;
543
544 return 0;
545}
546
547/**
548 * vidioc_g_input - return input line
Federico Vaga8dc97ea2013-02-05 14:34:37 -0300549 * @file: descriptor of device
Federico Vagaefeb98b2012-04-12 12:39:38 -0300550 * @i: returned input line number
551 *
552 * the current active input line is returned
553 *
554 * return value: always 0.
555 */
556static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
557{
Federico Vaga8dc97ea2013-02-05 14:34:37 -0300558 struct sta2x11_vip *vip = video_drvdata(file);
Federico Vagaefeb98b2012-04-12 12:39:38 -0300559
560 *i = vip->input;
561 return 0;
562}
563
564/**
565 * vidioc_enum_fmt_vid_cap - return video capture format
Federico Vagaefeb98b2012-04-12 12:39:38 -0300566 * @f: returned format information
567 *
568 * returns name and format of video capture
569 * Only UYVY is supported by hardware.
570 *
571 * return value: always 0.
572 */
573static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
574 struct v4l2_fmtdesc *f)
575{
576
577 if (f->index != 0)
578 return -EINVAL;
579
580 strcpy(f->description, "4:2:2, packed, UYVY");
581 f->pixelformat = V4L2_PIX_FMT_UYVY;
582 f->flags = 0;
583 return 0;
584}
585
586/**
587 * vidioc_try_fmt_vid_cap - set video capture format
Federico Vaga8dc97ea2013-02-05 14:34:37 -0300588 * @file: descriptor of device
Federico Vagaefeb98b2012-04-12 12:39:38 -0300589 * @f: new format
590 *
591 * new video format is set which includes width and
592 * field type. width is fixed to 720, no scaling.
593 * Only UYVY is supported by this hardware.
594 * the minimum height is 200, the maximum is 576 (PAL)
595 *
596 * return value: 0, no error
597 *
598 * -EINVAL, pixel or field format not supported
599 *
600 */
601static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
602 struct v4l2_format *f)
603{
Federico Vaga8dc97ea2013-02-05 14:34:37 -0300604 struct sta2x11_vip *vip = video_drvdata(file);
Federico Vagaefeb98b2012-04-12 12:39:38 -0300605 int interlace_lim;
606
Federico Vaga8dc97ea2013-02-05 14:34:37 -0300607 if (V4L2_PIX_FMT_UYVY != f->fmt.pix.pixelformat) {
608 v4l2_warn(&vip->v4l2_dev, "Invalid format, only UYVY supported\n");
Federico Vagaefeb98b2012-04-12 12:39:38 -0300609 return -EINVAL;
Federico Vaga8dc97ea2013-02-05 14:34:37 -0300610 }
Federico Vagaefeb98b2012-04-12 12:39:38 -0300611
Mauro Carvalho Chehab6ae009a2012-05-20 12:07:41 -0300612 if (V4L2_STD_525_60 & vip->std)
Federico Vagaefeb98b2012-04-12 12:39:38 -0300613 interlace_lim = 240;
614 else
615 interlace_lim = 288;
616
617 switch (f->fmt.pix.field) {
Federico Vaga8dc97ea2013-02-05 14:34:37 -0300618 default:
Federico Vagaefeb98b2012-04-12 12:39:38 -0300619 case V4L2_FIELD_ANY:
620 if (interlace_lim < f->fmt.pix.height)
621 f->fmt.pix.field = V4L2_FIELD_INTERLACED;
622 else
623 f->fmt.pix.field = V4L2_FIELD_BOTTOM;
624 break;
625 case V4L2_FIELD_TOP:
626 case V4L2_FIELD_BOTTOM:
627 if (interlace_lim < f->fmt.pix.height)
628 f->fmt.pix.height = interlace_lim;
629 break;
630 case V4L2_FIELD_INTERLACED:
631 break;
Federico Vagaefeb98b2012-04-12 12:39:38 -0300632 }
633
Federico Vaga8dc97ea2013-02-05 14:34:37 -0300634 /* It is the only supported format */
635 f->fmt.pix.pixelformat = V4L2_PIX_FMT_UYVY;
Federico Vagaefeb98b2012-04-12 12:39:38 -0300636 f->fmt.pix.height &= ~1;
637 if (2 * interlace_lim < f->fmt.pix.height)
638 f->fmt.pix.height = 2 * interlace_lim;
639 if (200 > f->fmt.pix.height)
640 f->fmt.pix.height = 200;
641 f->fmt.pix.width = 720;
642 f->fmt.pix.bytesperline = f->fmt.pix.width * 2;
643 f->fmt.pix.sizeimage = f->fmt.pix.width * 2 * f->fmt.pix.height;
644 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
645 f->fmt.pix.priv = 0;
646 return 0;
647}
648
649/**
650 * vidioc_s_fmt_vid_cap - set current video format parameters
Federico Vaga8dc97ea2013-02-05 14:34:37 -0300651 * @file: descriptor of device
Federico Vagaefeb98b2012-04-12 12:39:38 -0300652 * @f: returned format information
653 *
654 * set new capture format
655 * return value: 0, no error
656 *
657 * other, delivered by video DAC routine.
658 */
659static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
660 struct v4l2_format *f)
661{
Federico Vaga8dc97ea2013-02-05 14:34:37 -0300662 struct sta2x11_vip *vip = video_drvdata(file);
663 unsigned int t_stop, b_stop, pitch;
Federico Vagaefeb98b2012-04-12 12:39:38 -0300664 int ret;
665
666 ret = vidioc_try_fmt_vid_cap(file, priv, f);
667 if (ret)
668 return ret;
669
Federico Vaga8dc97ea2013-02-05 14:34:37 -0300670 if (vb2_is_busy(&vip->vb_vidq)) {
671 /* Can't change format during acquisition */
672 v4l2_err(&vip->v4l2_dev, "device busy\n");
673 return -EBUSY;
674 }
675 vip->format = f->fmt.pix;
676 switch (vip->format.field) {
677 case V4L2_FIELD_INTERLACED:
678 t_stop = ((vip->format.height / 2 - 1) << 16) |
679 (2 * vip->format.width - 1);
680 b_stop = t_stop;
681 pitch = 4 * vip->format.width;
682 break;
683 case V4L2_FIELD_TOP:
684 t_stop = ((vip->format.height - 1) << 16) |
685 (2 * vip->format.width - 1);
686 b_stop = (0 << 16) | (2 * vip->format.width - 1);
687 pitch = 2 * vip->format.width;
688 break;
689 case V4L2_FIELD_BOTTOM:
690 t_stop = (0 << 16) | (2 * vip->format.width - 1);
691 b_stop = (vip->format.height << 16) |
692 (2 * vip->format.width - 1);
693 pitch = 2 * vip->format.width;
694 break;
695 default:
696 v4l2_err(&vip->v4l2_dev, "unknown field format\n");
697 return -EINVAL;
698 }
699
700 spin_lock_irq(&vip->slock);
701 /* Y-X Top Field Offset */
702 reg_write(vip, DVP_TFO, 0);
703 /* Y-X Bottom Field Offset */
704 reg_write(vip, DVP_BFO, 0);
705 /* Y-X Top Field Stop*/
706 reg_write(vip, DVP_TFS, t_stop);
707 /* Y-X Bottom Field Stop */
708 reg_write(vip, DVP_BFS, b_stop);
709 /* Video Memory Pitch */
710 reg_write(vip, DVP_VMP, pitch);
711 spin_unlock_irq(&vip->slock);
712
Federico Vagaefeb98b2012-04-12 12:39:38 -0300713 return 0;
714}
715
716/**
717 * vidioc_g_fmt_vid_cap - get current video format parameters
Federico Vaga8dc97ea2013-02-05 14:34:37 -0300718 * @file: descriptor of device
Federico Vagaefeb98b2012-04-12 12:39:38 -0300719 * @f: contains format information
720 *
721 * returns current video format parameters
722 *
723 * return value: 0, always successful
724 */
725static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
726 struct v4l2_format *f)
727{
Federico Vaga8dc97ea2013-02-05 14:34:37 -0300728 struct sta2x11_vip *vip = video_drvdata(file);
Federico Vagaefeb98b2012-04-12 12:39:38 -0300729
Federico Vaga8dc97ea2013-02-05 14:34:37 -0300730 f->fmt.pix = vip->format;
731
Federico Vagaefeb98b2012-04-12 12:39:38 -0300732 return 0;
733}
734
Federico Vagaefeb98b2012-04-12 12:39:38 -0300735static const struct v4l2_ioctl_ops vip_ioctl_ops = {
736 .vidioc_querycap = vidioc_querycap,
Federico Vaga8dc97ea2013-02-05 14:34:37 -0300737 /* FMT handling */
Federico Vagaefeb98b2012-04-12 12:39:38 -0300738 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
Federico Vagaefeb98b2012-04-12 12:39:38 -0300739 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
Federico Vaga8dc97ea2013-02-05 14:34:37 -0300740 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
741 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
742 /* Buffer handlers */
743 .vidioc_create_bufs = vb2_ioctl_create_bufs,
744 .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
745 .vidioc_reqbufs = vb2_ioctl_reqbufs,
746 .vidioc_querybuf = vb2_ioctl_querybuf,
747 .vidioc_qbuf = vb2_ioctl_qbuf,
748 .vidioc_dqbuf = vb2_ioctl_dqbuf,
749 /* Stream on/off */
750 .vidioc_streamon = vb2_ioctl_streamon,
751 .vidioc_streamoff = vb2_ioctl_streamoff,
752 /* Standard handling */
753 .vidioc_g_std = vidioc_g_std,
754 .vidioc_s_std = vidioc_s_std,
755 .vidioc_querystd = vidioc_querystd,
756 /* Input handling */
757 .vidioc_enum_input = vidioc_enum_input,
758 .vidioc_g_input = vidioc_g_input,
759 .vidioc_s_input = vidioc_s_input,
760 /* Log status ioctl */
761 .vidioc_log_status = v4l2_ctrl_log_status,
762 /* Event handling */
763 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
764 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
Federico Vagaefeb98b2012-04-12 12:39:38 -0300765};
766
767static struct video_device video_dev_template = {
Federico Vaga8dc97ea2013-02-05 14:34:37 -0300768 .name = KBUILD_MODNAME,
Federico Vagaefeb98b2012-04-12 12:39:38 -0300769 .release = video_device_release,
770 .fops = &vip_fops,
771 .ioctl_ops = &vip_ioctl_ops,
772 .tvnorms = V4L2_STD_ALL,
773};
774
775/**
776 * vip_irq - interrupt routine
777 * @irq: Number of interrupt ( not used, correct number is assumed )
778 * @vip: local data structure containing all information
779 *
780 * check for both frame interrupts set ( top and bottom ).
781 * check FIFO overflow, but limit number of log messages after open.
Federico Vaga8dc97ea2013-02-05 14:34:37 -0300782 * signal a complete buffer if done
Federico Vagaefeb98b2012-04-12 12:39:38 -0300783 *
784 * return value: IRQ_NONE, interrupt was not generated by VIP
785 *
786 * IRQ_HANDLED, interrupt done.
787 */
788static irqreturn_t vip_irq(int irq, struct sta2x11_vip *vip)
789{
Federico Vaga8dc97ea2013-02-05 14:34:37 -0300790 unsigned int status;
Federico Vagaefeb98b2012-04-12 12:39:38 -0300791
Federico Vaga8dc97ea2013-02-05 14:34:37 -0300792 status = reg_read(vip, DVP_ITS);
Federico Vagaefeb98b2012-04-12 12:39:38 -0300793
Federico Vaga8dc97ea2013-02-05 14:34:37 -0300794 if (!status) /* No interrupt to handle */
Federico Vagaefeb98b2012-04-12 12:39:38 -0300795 return IRQ_NONE;
Federico Vagaefeb98b2012-04-12 12:39:38 -0300796
Federico Vaga8dc97ea2013-02-05 14:34:37 -0300797 if (status & DVP_IT_FIFO)
798 if (vip->overflow++ > 5)
799 pr_info("VIP: fifo overflow\n");
Federico Vagaefeb98b2012-04-12 12:39:38 -0300800
Federico Vaga8dc97ea2013-02-05 14:34:37 -0300801 if ((status & DVP_IT_VST) && (status & DVP_IT_VSB)) {
Federico Vagaefeb98b2012-04-12 12:39:38 -0300802 /* this is bad, we are too slow, hope the condition is gone
803 * on the next frame */
Federico Vagaefeb98b2012-04-12 12:39:38 -0300804 return IRQ_HANDLED;
805 }
806
Federico Vaga8dc97ea2013-02-05 14:34:37 -0300807 if (status & DVP_IT_VST)
808 if ((++vip->tcount) < 2)
809 return IRQ_HANDLED;
810 if (status & DVP_IT_VSB) {
811 vip->bcount++;
Federico Vagaefeb98b2012-04-12 12:39:38 -0300812 return IRQ_HANDLED;
Federico Vagaefeb98b2012-04-12 12:39:38 -0300813 }
Federico Vagaefeb98b2012-04-12 12:39:38 -0300814
Federico Vaga8dc97ea2013-02-05 14:34:37 -0300815 if (vip->active) { /* Acquisition is over on this buffer */
816 /* Disable acquisition */
817 reg_write(vip, DVP_CTL, reg_read(vip, DVP_CTL) & ~DVP_CTL_ENA);
818 /* Remove the active buffer from the list */
819 do_gettimeofday(&vip->active->vb.v4l2_buf.timestamp);
820 vip->active->vb.v4l2_buf.sequence = vip->sequence++;
821 vb2_buffer_done(&vip->active->vb, VB2_BUF_STATE_DONE);
Federico Vagaefeb98b2012-04-12 12:39:38 -0300822 }
Federico Vaga8dc97ea2013-02-05 14:34:37 -0300823
Federico Vagaefeb98b2012-04-12 12:39:38 -0300824 return IRQ_HANDLED;
825}
826
Federico Vaga8dc97ea2013-02-05 14:34:37 -0300827static void sta2x11_vip_init_register(struct sta2x11_vip *vip)
828{
829 /* Register initialization */
830 spin_lock_irq(&vip->slock);
831 /* Clean interrupt */
832 reg_read(vip, DVP_ITS);
833 /* Enable Half Line per vertical */
834 reg_write(vip, DVP_HLFLN, DVP_HLFLN_SD);
835 /* Reset VIP control */
836 reg_write(vip, DVP_CTL, DVP_CTL_RST);
837 /* Clear VIP control */
838 reg_write(vip, DVP_CTL, 0);
839 spin_unlock_irq(&vip->slock);
840}
841static void sta2x11_vip_clear_register(struct sta2x11_vip *vip)
842{
843 spin_lock_irq(&vip->slock);
844 /* Disable interrupt */
845 reg_write(vip, DVP_ITM, 0);
846 /* Reset VIP Control */
847 reg_write(vip, DVP_CTL, DVP_CTL_RST);
848 /* Clear VIP Control */
849 reg_write(vip, DVP_CTL, 0);
850 /* Clean VIP Interrupt */
851 reg_read(vip, DVP_ITS);
852 spin_unlock_irq(&vip->slock);
853}
854static int sta2x11_vip_init_buffer(struct sta2x11_vip *vip)
855{
856 int err;
857
858 err = dma_set_coherent_mask(&vip->pdev->dev, DMA_BIT_MASK(29));
859 if (err) {
860 v4l2_err(&vip->v4l2_dev, "Cannot configure coherent mask");
861 return err;
862 }
863 memset(&vip->vb_vidq, 0, sizeof(struct vb2_queue));
864 vip->vb_vidq.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
865 vip->vb_vidq.io_modes = VB2_MMAP | VB2_READ;
866 vip->vb_vidq.drv_priv = vip;
867 vip->vb_vidq.buf_struct_size = sizeof(struct vip_buffer);
868 vip->vb_vidq.ops = &vip_video_qops;
869 vip->vb_vidq.mem_ops = &vb2_dma_contig_memops;
870 err = vb2_queue_init(&vip->vb_vidq);
871 if (err)
872 return err;
873 INIT_LIST_HEAD(&vip->buffer_list);
874 spin_lock_init(&vip->lock);
875
876
877 vip->alloc_ctx = vb2_dma_contig_init_ctx(&vip->pdev->dev);
878 if (IS_ERR(vip->alloc_ctx)) {
879 v4l2_err(&vip->v4l2_dev, "Can't allocate buffer context");
880 return PTR_ERR(vip->alloc_ctx);
881 }
882
883 return 0;
884}
885static void sta2x11_vip_release_buffer(struct sta2x11_vip *vip)
886{
887 vb2_dma_contig_cleanup_ctx(vip->alloc_ctx);
888}
889static int sta2x11_vip_init_controls(struct sta2x11_vip *vip)
890{
891 /*
892 * Inititialize an empty control so VIP can inerithing controls
893 * from ADV7180
894 */
895 v4l2_ctrl_handler_init(&vip->ctrl_hdl, 0);
896
897 vip->v4l2_dev.ctrl_handler = &vip->ctrl_hdl;
898 if (vip->ctrl_hdl.error) {
899 int err = vip->ctrl_hdl.error;
900
901 v4l2_ctrl_handler_free(&vip->ctrl_hdl);
902 return err;
903 }
904
905 return 0;
906}
907
Federico Vagaefeb98b2012-04-12 12:39:38 -0300908/**
909 * vip_gpio_reserve - reserve gpio pin
910 * @dev: device
911 * @pin: GPIO pin number
912 * @dir: direction, input or output
913 * @name: GPIO pin name
914 *
915 */
916static int vip_gpio_reserve(struct device *dev, int pin, int dir,
917 const char *name)
918{
919 int ret;
920
921 if (pin == -1)
922 return 0;
923
924 ret = gpio_request(pin, name);
925 if (ret) {
926 dev_err(dev, "Failed to allocate pin %d (%s)\n", pin, name);
927 return ret;
928 }
929
930 ret = gpio_direction_output(pin, dir);
931 if (ret) {
932 dev_err(dev, "Failed to set direction for pin %d (%s)\n",
933 pin, name);
934 gpio_free(pin);
935 return ret;
936 }
937
938 ret = gpio_export(pin, false);
939 if (ret) {
940 dev_err(dev, "Failed to export pin %d (%s)\n", pin, name);
941 gpio_free(pin);
942 return ret;
943 }
944
945 return 0;
946}
947
948/**
949 * vip_gpio_release - release gpio pin
950 * @dev: device
951 * @pin: GPIO pin number
952 * @name: GPIO pin name
953 *
954 */
955static void vip_gpio_release(struct device *dev, int pin, const char *name)
956{
957 if (pin != -1) {
958 dev_dbg(dev, "releasing pin %d (%s)\n", pin, name);
959 gpio_unexport(pin);
960 gpio_free(pin);
961 }
962}
963
964/**
965 * sta2x11_vip_init_one - init one instance of video device
966 * @pdev: PCI device
967 * @ent: (not used)
968 *
969 * allocate reset pins for DAC.
970 * Reset video DAC, this is done via reset line.
971 * allocate memory for managing device
972 * request interrupt
973 * map IO region
974 * register device
975 * find and initialize video DAC
976 *
977 * return value: 0, no error
978 *
979 * -ENOMEM, no memory
980 *
981 * -ENODEV, device could not be detected or registered
982 */
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -0800983static int sta2x11_vip_init_one(struct pci_dev *pdev,
984 const struct pci_device_id *ent)
Federico Vagaefeb98b2012-04-12 12:39:38 -0300985{
986 int ret;
987 struct sta2x11_vip *vip;
988 struct vip_config *config;
989
Federico Vaga8dc97ea2013-02-05 14:34:37 -0300990 /* Check if hardware support 26-bit DMA */
991 if (dma_set_mask(&pdev->dev, DMA_BIT_MASK(26))) {
992 dev_err(&pdev->dev, "26-bit DMA addressing not available\n");
993 return -EINVAL;
994 }
995 /* Enable PCI */
Federico Vagaefeb98b2012-04-12 12:39:38 -0300996 ret = pci_enable_device(pdev);
997 if (ret)
998 return ret;
999
Federico Vaga8dc97ea2013-02-05 14:34:37 -03001000 /* Get VIP platform data */
Federico Vagaefeb98b2012-04-12 12:39:38 -03001001 config = dev_get_platdata(&pdev->dev);
1002 if (!config) {
1003 dev_info(&pdev->dev, "VIP slot disabled\n");
1004 ret = -EINVAL;
1005 goto disable;
1006 }
1007
Federico Vaga8dc97ea2013-02-05 14:34:37 -03001008 /* Power configuration */
Federico Vagaefeb98b2012-04-12 12:39:38 -03001009 ret = vip_gpio_reserve(&pdev->dev, config->pwr_pin, 0,
1010 config->pwr_name);
1011 if (ret)
1012 goto disable;
1013
1014 if (config->reset_pin >= 0) {
1015 ret = vip_gpio_reserve(&pdev->dev, config->reset_pin, 0,
1016 config->reset_name);
1017 if (ret) {
1018 vip_gpio_release(&pdev->dev, config->pwr_pin,
1019 config->pwr_name);
1020 goto disable;
1021 }
1022 }
Federico Vagaefeb98b2012-04-12 12:39:38 -03001023 if (config->pwr_pin != -1) {
1024 /* Datasheet says 5ms between PWR and RST */
1025 usleep_range(5000, 25000);
1026 ret = gpio_direction_output(config->pwr_pin, 1);
1027 }
1028
1029 if (config->reset_pin != -1) {
1030 /* Datasheet says 5ms between PWR and RST */
1031 usleep_range(5000, 25000);
1032 ret = gpio_direction_output(config->reset_pin, 1);
1033 }
1034 usleep_range(5000, 25000);
1035
Federico Vaga8dc97ea2013-02-05 14:34:37 -03001036 /* Allocate a new VIP instance */
Federico Vagaefeb98b2012-04-12 12:39:38 -03001037 vip = kzalloc(sizeof(struct sta2x11_vip), GFP_KERNEL);
1038 if (!vip) {
1039 ret = -ENOMEM;
1040 goto release_gpios;
1041 }
Federico Vagaefeb98b2012-04-12 12:39:38 -03001042 vip->pdev = pdev;
1043 vip->std = V4L2_STD_PAL;
1044 vip->format = formats_50[0];
1045 vip->config = config;
1046
Federico Vaga8dc97ea2013-02-05 14:34:37 -03001047 ret = sta2x11_vip_init_controls(vip);
1048 if (ret)
1049 goto free_mem;
Wei Yongjund0176502013-05-13 10:00:01 -03001050 ret = v4l2_device_register(&pdev->dev, &vip->v4l2_dev);
1051 if (ret)
Federico Vagaefeb98b2012-04-12 12:39:38 -03001052 goto free_mem;
1053
1054 dev_dbg(&pdev->dev, "BAR #0 at 0x%lx 0x%lx irq %d\n",
1055 (unsigned long)pci_resource_start(pdev, 0),
1056 (unsigned long)pci_resource_len(pdev, 0), pdev->irq);
1057
1058 pci_set_master(pdev);
1059
Federico Vaga8dc97ea2013-02-05 14:34:37 -03001060 ret = pci_request_regions(pdev, KBUILD_MODNAME);
Federico Vagaefeb98b2012-04-12 12:39:38 -03001061 if (ret)
1062 goto unreg;
1063
1064 vip->iomem = pci_iomap(pdev, 0, 0x100);
1065 if (!vip->iomem) {
Federico Vaga8dc97ea2013-02-05 14:34:37 -03001066 ret = -ENOMEM;
Federico Vagaefeb98b2012-04-12 12:39:38 -03001067 goto release;
1068 }
1069
1070 pci_enable_msi(pdev);
1071
Federico Vaga8dc97ea2013-02-05 14:34:37 -03001072 /* Initialize buffer */
1073 ret = sta2x11_vip_init_buffer(vip);
1074 if (ret)
1075 goto unmap;
1076
Federico Vagaefeb98b2012-04-12 12:39:38 -03001077 spin_lock_init(&vip->slock);
Federico Vagaefeb98b2012-04-12 12:39:38 -03001078
1079 ret = request_irq(pdev->irq,
1080 (irq_handler_t) vip_irq,
Federico Vaga8dc97ea2013-02-05 14:34:37 -03001081 IRQF_SHARED, KBUILD_MODNAME, vip);
Federico Vagaefeb98b2012-04-12 12:39:38 -03001082 if (ret) {
1083 dev_err(&pdev->dev, "request_irq failed\n");
1084 ret = -ENODEV;
Federico Vaga8dc97ea2013-02-05 14:34:37 -03001085 goto release_buf;
Federico Vagaefeb98b2012-04-12 12:39:38 -03001086 }
1087
Federico Vaga8dc97ea2013-02-05 14:34:37 -03001088 /* Alloc, initialize and register video device */
Federico Vagaefeb98b2012-04-12 12:39:38 -03001089 vip->video_dev = video_device_alloc();
1090 if (!vip->video_dev) {
1091 ret = -ENOMEM;
1092 goto release_irq;
1093 }
1094
Federico Vaga8dc97ea2013-02-05 14:34:37 -03001095 vip->video_dev = &video_dev_template;
1096 vip->video_dev->v4l2_dev = &vip->v4l2_dev;
1097 vip->video_dev->queue = &vip->vb_vidq;
1098 set_bit(V4L2_FL_USE_FH_PRIO, &vip->video_dev->flags);
Federico Vagaefeb98b2012-04-12 12:39:38 -03001099 video_set_drvdata(vip->video_dev, vip);
1100
1101 ret = video_register_device(vip->video_dev, VFL_TYPE_GRABBER, -1);
1102 if (ret)
1103 goto vrelease;
1104
Federico Vaga8dc97ea2013-02-05 14:34:37 -03001105 /* Get ADV7180 subdevice */
Federico Vagaefeb98b2012-04-12 12:39:38 -03001106 vip->adapter = i2c_get_adapter(vip->config->i2c_id);
1107 if (!vip->adapter) {
1108 ret = -ENODEV;
1109 dev_err(&pdev->dev, "no I2C adapter found\n");
1110 goto vunreg;
1111 }
1112
1113 vip->decoder = v4l2_i2c_new_subdev(&vip->v4l2_dev, vip->adapter,
1114 "adv7180", vip->config->i2c_addr,
1115 NULL);
1116 if (!vip->decoder) {
1117 ret = -ENODEV;
1118 dev_err(&pdev->dev, "no decoder found\n");
1119 goto vunreg;
1120 }
1121
1122 i2c_put_adapter(vip->adapter);
Federico Vagaefeb98b2012-04-12 12:39:38 -03001123 v4l2_subdev_call(vip->decoder, core, init, 0);
1124
Federico Vaga8dc97ea2013-02-05 14:34:37 -03001125 sta2x11_vip_init_register(vip);
1126
1127 dev_info(&pdev->dev, "STA2X11 Video Input Port (VIP) loaded\n");
Federico Vagaefeb98b2012-04-12 12:39:38 -03001128 return 0;
1129
1130vunreg:
1131 video_set_drvdata(vip->video_dev, NULL);
1132vrelease:
1133 if (video_is_registered(vip->video_dev))
1134 video_unregister_device(vip->video_dev);
1135 else
1136 video_device_release(vip->video_dev);
1137release_irq:
1138 free_irq(pdev->irq, vip);
Federico Vaga8dc97ea2013-02-05 14:34:37 -03001139release_buf:
1140 sta2x11_vip_release_buffer(vip);
Federico Vagaefeb98b2012-04-12 12:39:38 -03001141 pci_disable_msi(pdev);
1142unmap:
Federico Vaga8dc97ea2013-02-05 14:34:37 -03001143 vb2_queue_release(&vip->vb_vidq);
Federico Vagaefeb98b2012-04-12 12:39:38 -03001144 pci_iounmap(pdev, vip->iomem);
Federico Vagaefeb98b2012-04-12 12:39:38 -03001145release:
1146 pci_release_regions(pdev);
1147unreg:
1148 v4l2_device_unregister(&vip->v4l2_dev);
1149free_mem:
1150 kfree(vip);
1151release_gpios:
1152 vip_gpio_release(&pdev->dev, config->reset_pin, config->reset_name);
1153 vip_gpio_release(&pdev->dev, config->pwr_pin, config->pwr_name);
1154disable:
1155 /*
1156 * do not call pci_disable_device on sta2x11 because it break all
1157 * other Bus masters on this EP
1158 */
1159 return ret;
1160}
1161
1162/**
1163 * sta2x11_vip_remove_one - release device
1164 * @pdev: PCI device
1165 *
1166 * Undo everything done in .._init_one
1167 *
1168 * unregister video device
1169 * free interrupt
1170 * unmap ioadresses
1171 * free memory
1172 * free GPIO pins
1173 */
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08001174static void sta2x11_vip_remove_one(struct pci_dev *pdev)
Federico Vagaefeb98b2012-04-12 12:39:38 -03001175{
1176 struct v4l2_device *v4l2_dev = pci_get_drvdata(pdev);
1177 struct sta2x11_vip *vip =
1178 container_of(v4l2_dev, struct sta2x11_vip, v4l2_dev);
1179
Federico Vaga8dc97ea2013-02-05 14:34:37 -03001180 sta2x11_vip_clear_register(vip);
1181
Federico Vagaefeb98b2012-04-12 12:39:38 -03001182 video_set_drvdata(vip->video_dev, NULL);
1183 video_unregister_device(vip->video_dev);
1184 /*do not call video_device_release() here, is already done */
1185 free_irq(pdev->irq, vip);
1186 pci_disable_msi(pdev);
Federico Vaga8dc97ea2013-02-05 14:34:37 -03001187 vb2_queue_release(&vip->vb_vidq);
Federico Vagaefeb98b2012-04-12 12:39:38 -03001188 pci_iounmap(pdev, vip->iomem);
1189 pci_release_regions(pdev);
1190
1191 v4l2_device_unregister(&vip->v4l2_dev);
Federico Vagaefeb98b2012-04-12 12:39:38 -03001192
1193 vip_gpio_release(&pdev->dev, vip->config->pwr_pin,
1194 vip->config->pwr_name);
1195 vip_gpio_release(&pdev->dev, vip->config->reset_pin,
1196 vip->config->reset_name);
1197
1198 kfree(vip);
1199 /*
1200 * do not call pci_disable_device on sta2x11 because it break all
1201 * other Bus masters on this EP
1202 */
1203}
1204
1205#ifdef CONFIG_PM
1206
1207/**
1208 * sta2x11_vip_suspend - set device into power save mode
1209 * @pdev: PCI device
1210 * @state: new state of device
1211 *
1212 * all relevant registers are saved and an attempt to set a new state is made.
1213 *
1214 * return value: 0 always indicate success,
1215 * even if device could not be disabled. (workaround for hardware problem)
Federico Vagaefeb98b2012-04-12 12:39:38 -03001216 */
1217static int sta2x11_vip_suspend(struct pci_dev *pdev, pm_message_t state)
1218{
1219 struct v4l2_device *v4l2_dev = pci_get_drvdata(pdev);
1220 struct sta2x11_vip *vip =
1221 container_of(v4l2_dev, struct sta2x11_vip, v4l2_dev);
1222 unsigned long flags;
1223 int i;
1224
1225 spin_lock_irqsave(&vip->slock, flags);
Federico Vaga8dc97ea2013-02-05 14:34:37 -03001226 vip->register_save_area[0] = reg_read(vip, DVP_CTL);
1227 reg_write(vip, DVP_CTL, vip->register_save_area[0] & DVP_CTL_DIS);
1228 vip->register_save_area[SAVE_COUNT] = reg_read(vip, DVP_ITM);
1229 reg_write(vip, DVP_ITM, 0);
Federico Vagaefeb98b2012-04-12 12:39:38 -03001230 for (i = 1; i < SAVE_COUNT; i++)
Federico Vaga8dc97ea2013-02-05 14:34:37 -03001231 vip->register_save_area[i] = reg_read(vip, 4 * i);
Federico Vagaefeb98b2012-04-12 12:39:38 -03001232 for (i = 0; i < AUX_COUNT; i++)
1233 vip->register_save_area[SAVE_COUNT + IRQ_COUNT + i] =
Federico Vaga8dc97ea2013-02-05 14:34:37 -03001234 reg_read(vip, registers_to_save[i]);
Federico Vagaefeb98b2012-04-12 12:39:38 -03001235 spin_unlock_irqrestore(&vip->slock, flags);
1236 /* save pci state */
1237 pci_save_state(pdev);
1238 if (pci_set_power_state(pdev, pci_choose_state(pdev, state))) {
1239 /*
1240 * do not call pci_disable_device on sta2x11 because it
1241 * break all other Bus masters on this EP
1242 */
1243 vip->disabled = 1;
1244 }
1245
1246 pr_info("VIP: suspend\n");
1247 return 0;
1248}
1249
1250/**
1251 * sta2x11_vip_resume - resume device operation
1252 * @pdev : PCI device
1253 *
1254 * re-enable device, set PCI state to powered and restore registers.
1255 * resume normal device operation afterwards.
1256 *
1257 * return value: 0, no error.
1258 *
1259 * other, could not set device to power on state.
1260 */
1261static int sta2x11_vip_resume(struct pci_dev *pdev)
1262{
1263 struct v4l2_device *v4l2_dev = pci_get_drvdata(pdev);
1264 struct sta2x11_vip *vip =
1265 container_of(v4l2_dev, struct sta2x11_vip, v4l2_dev);
1266 unsigned long flags;
1267 int ret, i;
1268
1269 pr_info("VIP: resume\n");
1270 /* restore pci state */
1271 if (vip->disabled) {
1272 ret = pci_enable_device(pdev);
1273 if (ret) {
Federico Vaga8dc97ea2013-02-05 14:34:37 -03001274 pr_warn("VIP: Can't enable device.\n");
Federico Vagaefeb98b2012-04-12 12:39:38 -03001275 return ret;
1276 }
1277 vip->disabled = 0;
1278 }
1279 ret = pci_set_power_state(pdev, PCI_D0);
1280 if (ret) {
1281 /*
1282 * do not call pci_disable_device on sta2x11 because it
1283 * break all other Bus masters on this EP
1284 */
Federico Vaga8dc97ea2013-02-05 14:34:37 -03001285 pr_warn("VIP: Can't enable device.\n");
Federico Vagaefeb98b2012-04-12 12:39:38 -03001286 vip->disabled = 1;
1287 return ret;
1288 }
1289
1290 pci_restore_state(pdev);
1291
1292 spin_lock_irqsave(&vip->slock, flags);
1293 for (i = 1; i < SAVE_COUNT; i++)
Federico Vaga8dc97ea2013-02-05 14:34:37 -03001294 reg_write(vip, 4 * i, vip->register_save_area[i]);
Federico Vagaefeb98b2012-04-12 12:39:38 -03001295 for (i = 0; i < AUX_COUNT; i++)
Federico Vaga8dc97ea2013-02-05 14:34:37 -03001296 reg_write(vip, registers_to_save[i],
Federico Vagaefeb98b2012-04-12 12:39:38 -03001297 vip->register_save_area[SAVE_COUNT + IRQ_COUNT + i]);
Federico Vaga8dc97ea2013-02-05 14:34:37 -03001298 reg_write(vip, DVP_CTL, vip->register_save_area[0]);
1299 reg_write(vip, DVP_ITM, vip->register_save_area[SAVE_COUNT]);
Federico Vagaefeb98b2012-04-12 12:39:38 -03001300 spin_unlock_irqrestore(&vip->slock, flags);
1301 return 0;
1302}
1303
1304#endif
1305
1306static DEFINE_PCI_DEVICE_TABLE(sta2x11_vip_pci_tbl) = {
1307 {PCI_DEVICE(PCI_VENDOR_ID_STMICRO, PCI_DEVICE_ID_STMICRO_VIP)},
1308 {0,}
1309};
1310
1311static struct pci_driver sta2x11_vip_driver = {
Federico Vaga8dc97ea2013-02-05 14:34:37 -03001312 .name = KBUILD_MODNAME,
Federico Vagaefeb98b2012-04-12 12:39:38 -03001313 .probe = sta2x11_vip_init_one,
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08001314 .remove = sta2x11_vip_remove_one,
Federico Vagaefeb98b2012-04-12 12:39:38 -03001315 .id_table = sta2x11_vip_pci_tbl,
1316#ifdef CONFIG_PM
1317 .suspend = sta2x11_vip_suspend,
1318 .resume = sta2x11_vip_resume,
1319#endif
1320};
1321
1322static int __init sta2x11_vip_init_module(void)
1323{
1324 return pci_register_driver(&sta2x11_vip_driver);
1325}
1326
1327static void __exit sta2x11_vip_exit_module(void)
1328{
1329 pci_unregister_driver(&sta2x11_vip_driver);
1330}
1331
1332#ifdef MODULE
1333module_init(sta2x11_vip_init_module);
1334module_exit(sta2x11_vip_exit_module);
1335#else
1336late_initcall_sync(sta2x11_vip_init_module);
1337#endif
1338
1339MODULE_DESCRIPTION("STA2X11 Video Input Port driver");
1340MODULE_AUTHOR("Wind River");
1341MODULE_LICENSE("GPL v2");
1342MODULE_SUPPORTED_DEVICE("sta2x11 video input");
1343MODULE_VERSION(DRV_VERSION);
1344MODULE_DEVICE_TABLE(pci, sta2x11_vip_pci_tbl);