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