1/*
2 *  tw68 driver common header file
3 *
4 *  Much of this code is derived from the cx88 and sa7134 drivers, which
5 *  were in turn derived from the bt87x driver.  The original work was by
6 *  Gerd Knorr; more recently the code was enhanced by Mauro Carvalho Chehab,
7 *  Hans Verkuil, Andy Walls and many others.  Their work is gratefully
8 *  acknowledged.  Full credit goes to them - any problems within this code
9 *  are mine.
10 *
11 *  Copyright (C) 2009  William M. Brack
12 *
13 *  Refactored and updated to the latest v4l core frameworks:
14 *
15 *  Copyright (C) 2014 Hans Verkuil <hverkuil@xs4all.nl>
16 *
17 *  This program is free software; you can redistribute it and/or modify
18 *  it under the terms of the GNU General Public License as published by
19 *  the Free Software Foundation; either version 2 of the License, or
20 *  (at your option) any later version.
21 *
22 *  This program is distributed in the hope that it will be useful,
23 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
24 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25 *  GNU General Public License for more details.
26 */
27
28#include <linux/pci.h>
29#include <linux/videodev2.h>
30#include <linux/notifier.h>
31#include <linux/delay.h>
32#include <linux/mutex.h>
33#include <linux/io.h>
34
35#include <media/v4l2-common.h>
36#include <media/v4l2-ioctl.h>
37#include <media/v4l2-ctrls.h>
38#include <media/v4l2-device.h>
39#include <media/videobuf2-dma-sg.h>
40
41#include "tw68-reg.h"
42
43#define	UNSET	(-1U)
44
45/* system vendor and device ID's */
46#define	PCI_VENDOR_ID_TECHWELL	0x1797
47#define	PCI_DEVICE_ID_6800	0x6800
48#define	PCI_DEVICE_ID_6801	0x6801
49#define	PCI_DEVICE_ID_AUDIO2	0x6802
50#define	PCI_DEVICE_ID_TS3	0x6803
51#define	PCI_DEVICE_ID_6804	0x6804
52#define	PCI_DEVICE_ID_AUDIO5	0x6805
53#define	PCI_DEVICE_ID_TS6	0x6806
54
55/* tw6816 based cards */
56#define	PCI_DEVICE_ID_6816_1   0x6810
57#define	PCI_DEVICE_ID_6816_2   0x6811
58#define	PCI_DEVICE_ID_6816_3   0x6812
59#define	PCI_DEVICE_ID_6816_4   0x6813
60
61#define TW68_NORMS ( \
62	V4L2_STD_NTSC    | V4L2_STD_PAL       | V4L2_STD_SECAM    | \
63	V4L2_STD_PAL_M   | V4L2_STD_PAL_Nc    | V4L2_STD_PAL_60)
64
65#define	TW68_VID_INTS	(TW68_FFERR | TW68_PABORT | TW68_DMAPERR | \
66			 TW68_FFOF   | TW68_DMAPI)
67/* TW6800 chips have trouble with these, so we don't set them for that chip */
68#define	TW68_VID_INTSX	(TW68_FDMIS | TW68_HLOCK | TW68_VLOCK)
69
70#define	TW68_I2C_INTS	(TW68_SBERR | TW68_SBDONE | TW68_SBERR2  | \
71			 TW68_SBDONE2)
72
73enum tw68_decoder_type {
74	TW6800,
75	TW6801,
76	TW6804,
77	TWXXXX,
78};
79
80/* ----------------------------------------------------------- */
81/* static data                                                 */
82
83struct tw68_tvnorm {
84	char		*name;
85	v4l2_std_id	id;
86
87	/* video decoder */
88	u32	sync_control;
89	u32	luma_control;
90	u32	chroma_ctrl1;
91	u32	chroma_gain;
92	u32	chroma_ctrl2;
93	u32	vgate_misc;
94
95	/* video scaler */
96	u32	h_delay;
97	u32	h_delay0;	/* for TW6800 */
98	u32	h_start;
99	u32	h_stop;
100	u32	v_delay;
101	u32	video_v_start;
102	u32	video_v_stop;
103	u32	vbi_v_start_0;
104	u32	vbi_v_stop_0;
105	u32	vbi_v_start_1;
106
107	/* Techwell specific */
108	u32	format;
109};
110
111struct tw68_format {
112	char	*name;
113	u32	fourcc;
114	u32	depth;
115	u32	twformat;
116};
117
118/* ----------------------------------------------------------- */
119/* card configuration					  */
120
121#define TW68_BOARD_NOAUTO		UNSET
122#define TW68_BOARD_UNKNOWN		0
123#define	TW68_BOARD_GENERIC_6802		1
124
125#define	TW68_MAXBOARDS			16
126#define	TW68_INPUT_MAX			4
127
128/* ----------------------------------------------------------- */
129/* device / file handle status                                 */
130
131#define	BUFFER_TIMEOUT	msecs_to_jiffies(500)	/* 0.5 seconds */
132
133struct tw68_dev;	/* forward delclaration */
134
135/* buffer for one video/vbi/ts frame */
136struct tw68_buf {
137	struct vb2_buffer vb;
138	struct list_head list;
139
140	unsigned int   size;
141	__le32         *cpu;
142	__le32         *jmp;
143	dma_addr_t     dma;
144};
145
146struct tw68_fmt {
147	char			*name;
148	u32			fourcc;	/* v4l2 format id */
149	int			depth;
150	int			flags;
151	u32			twformat;
152};
153
154/* global device status */
155struct tw68_dev {
156	struct mutex		lock;
157	spinlock_t		slock;
158	u16			instance;
159	struct v4l2_device	v4l2_dev;
160
161	/* various device info */
162	enum tw68_decoder_type	vdecoder;
163	struct video_device	vdev;
164	struct v4l2_ctrl_handler hdl;
165
166	/* pci i/o */
167	char			*name;
168	struct pci_dev		*pci;
169	unsigned char		pci_rev, pci_lat;
170	u32			__iomem *lmmio;
171	u8			__iomem *bmmio;
172	u32			pci_irqmask;
173	/* The irq mask to be used will depend upon the chip type */
174	u32			board_virqmask;
175
176	/* video capture */
177	const struct tw68_format *fmt;
178	unsigned		width, height;
179	unsigned		seqnr;
180	unsigned		field;
181	struct vb2_queue	vidq;
182	struct list_head	active;
183	void			*alloc_ctx;
184
185	/* various v4l controls */
186	const struct tw68_tvnorm *tvnorm;	/* video */
187
188	int			input;
189};
190
191/* ----------------------------------------------------------- */
192
193#define tw_readl(reg)		readl(dev->lmmio + ((reg) >> 2))
194#define	tw_readb(reg)		readb(dev->bmmio + (reg))
195#define tw_writel(reg, value)	writel((value), dev->lmmio + ((reg) >> 2))
196#define	tw_writeb(reg, value)	writeb((value), dev->bmmio + (reg))
197
198#define tw_andorl(reg, mask, value) \
199		writel((readl(dev->lmmio+((reg)>>2)) & ~(mask)) |\
200		((value) & (mask)), dev->lmmio+((reg)>>2))
201#define	tw_andorb(reg, mask, value) \
202		writeb((readb(dev->bmmio + (reg)) & ~(mask)) |\
203		((value) & (mask)), dev->bmmio+(reg))
204#define tw_setl(reg, bit)	tw_andorl((reg), (bit), (bit))
205#define	tw_setb(reg, bit)	tw_andorb((reg), (bit), (bit))
206#define	tw_clearl(reg, bit)	\
207		writel((readl(dev->lmmio + ((reg) >> 2)) & ~(bit)), \
208		dev->lmmio + ((reg) >> 2))
209#define	tw_clearb(reg, bit)	\
210		writeb((readb(dev->bmmio+(reg)) & ~(bit)), \
211		dev->bmmio + (reg))
212
213#define tw_wait(us) { udelay(us); }
214
215/* ----------------------------------------------------------- */
216/* tw68-video.c                                                */
217
218void tw68_set_tvnorm_hw(struct tw68_dev *dev);
219
220int tw68_video_init1(struct tw68_dev *dev);
221int tw68_video_init2(struct tw68_dev *dev, int video_nr);
222void tw68_irq_video_done(struct tw68_dev *dev, unsigned long status);
223int tw68_video_start_dma(struct tw68_dev *dev, struct tw68_buf *buf);
224
225/* ----------------------------------------------------------- */
226/* tw68-risc.c                                                 */
227
228int tw68_risc_buffer(struct pci_dev *pci, struct tw68_buf *buf,
229	struct scatterlist *sglist, unsigned int top_offset,
230	unsigned int bottom_offset, unsigned int bpl,
231	unsigned int padding, unsigned int lines);
232