1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17#include <linux/init.h>
18#include <linux/list.h>
19#include <linux/module.h>
20#include <linux/kernel.h>
21#include <linux/slab.h>
22#include <linux/kmod.h>
23#include <linux/sound.h>
24#include <linux/interrupt.h>
25#include <linux/delay.h>
26#include <linux/dma-mapping.h>
27#include <linux/pm.h>
28#include <linux/pci_ids.h>
29#include <linux/jiffies.h>
30#include <asm/dma.h>
31#include <media/v4l2-dev.h>
32
33#include "tw5864.h"
34#include "tw5864-reg.h"
35
36MODULE_DESCRIPTION("V4L2 driver module for tw5864-based multimedia capture & encoding devices");
37MODULE_AUTHOR("Bluecherry Maintainers <maintainers@bluecherrydvr.com>");
38MODULE_AUTHOR("Andrey Utkin <andrey.utkin@corp.bluecherry.net>");
39MODULE_LICENSE("GPL");
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71static unsigned int video_nr[] = {[0 ... (TW5864_INPUTS - 1)] = -1 };
72
73module_param_array(video_nr, int, NULL, 0444);
74MODULE_PARM_DESC(video_nr, "video devices numbers array");
75
76
77
78
79
80
81static const struct pci_device_id tw5864_pci_tbl[] = {
82 {PCI_DEVICE(PCI_VENDOR_ID_TECHWELL, PCI_DEVICE_ID_TECHWELL_5864)},
83 {0,}
84};
85
86void tw5864_irqmask_apply(struct tw5864_dev *dev)
87{
88 tw_writel(TW5864_INTR_ENABLE_L, dev->irqmask & 0xffff);
89 tw_writel(TW5864_INTR_ENABLE_H, (dev->irqmask >> 16));
90}
91
92static void tw5864_interrupts_disable(struct tw5864_dev *dev)
93{
94 unsigned long flags;
95
96 spin_lock_irqsave(&dev->slock, flags);
97 dev->irqmask = 0;
98 tw5864_irqmask_apply(dev);
99 spin_unlock_irqrestore(&dev->slock, flags);
100}
101
102static void tw5864_timer_isr(struct tw5864_dev *dev);
103static void tw5864_h264_isr(struct tw5864_dev *dev);
104
105static irqreturn_t tw5864_isr(int irq, void *dev_id)
106{
107 struct tw5864_dev *dev = dev_id;
108 u32 status;
109
110 status = tw_readl(TW5864_INTR_STATUS_L) |
111 tw_readl(TW5864_INTR_STATUS_H) << 16;
112 if (!status)
113 return IRQ_NONE;
114
115 tw_writel(TW5864_INTR_CLR_L, 0xffff);
116 tw_writel(TW5864_INTR_CLR_H, 0xffff);
117
118 if (status & TW5864_INTR_VLC_DONE)
119 tw5864_h264_isr(dev);
120
121 if (status & TW5864_INTR_TIMER)
122 tw5864_timer_isr(dev);
123
124 if (!(status & (TW5864_INTR_TIMER | TW5864_INTR_VLC_DONE))) {
125 dev_dbg(&dev->pci->dev, "Unknown interrupt, status 0x%08X\n",
126 status);
127 }
128
129 return IRQ_HANDLED;
130}
131
132static void tw5864_h264_isr(struct tw5864_dev *dev)
133{
134 int channel = tw_readl(TW5864_DSP) & TW5864_DSP_ENC_CHN;
135 struct tw5864_input *input = &dev->inputs[channel];
136 int cur_frame_index, next_frame_index;
137 struct tw5864_h264_frame *cur_frame, *next_frame;
138 unsigned long flags;
139
140 spin_lock_irqsave(&dev->slock, flags);
141
142 cur_frame_index = dev->h264_buf_w_index;
143 next_frame_index = (cur_frame_index + 1) % H264_BUF_CNT;
144 cur_frame = &dev->h264_buf[cur_frame_index];
145 next_frame = &dev->h264_buf[next_frame_index];
146
147 if (next_frame_index != dev->h264_buf_r_index) {
148 cur_frame->vlc_len = tw_readl(TW5864_VLC_LENGTH) << 2;
149 cur_frame->checksum = tw_readl(TW5864_VLC_CRC_REG);
150 cur_frame->input = input;
151 cur_frame->timestamp = ktime_get_ns();
152 cur_frame->seqno = input->frame_seqno;
153 cur_frame->gop_seqno = input->frame_gop_seqno;
154
155 dev->h264_buf_w_index = next_frame_index;
156 tasklet_schedule(&dev->tasklet);
157
158 cur_frame = next_frame;
159
160 spin_lock(&input->slock);
161 input->frame_seqno++;
162 input->frame_gop_seqno++;
163 if (input->frame_gop_seqno >= input->gop)
164 input->frame_gop_seqno = 0;
165 spin_unlock(&input->slock);
166 } else {
167 dev_err(&dev->pci->dev,
168 "Skipped frame on input %d because all buffers busy\n",
169 channel);
170 }
171
172 dev->encoder_busy = 0;
173
174 spin_unlock_irqrestore(&dev->slock, flags);
175
176 tw_writel(TW5864_VLC_STREAM_BASE_ADDR, cur_frame->vlc.dma_addr);
177 tw_writel(TW5864_MV_STREAM_BASE_ADDR, cur_frame->mv.dma_addr);
178
179
180 tw_writel(TW5864_VLC_DSP_INTR, 0x00000001);
181 tw_writel(TW5864_PCI_INTR_STATUS, TW5864_VLC_DONE_INTR);
182}
183
184static void tw5864_input_deadline_update(struct tw5864_input *input)
185{
186 input->new_frame_deadline = jiffies + msecs_to_jiffies(1000);
187}
188
189static void tw5864_timer_isr(struct tw5864_dev *dev)
190{
191 unsigned long flags;
192 int i;
193 int encoder_busy;
194
195
196 tw_writel(TW5864_PCI_INTR_STATUS, TW5864_TIMER_INTR);
197
198 spin_lock_irqsave(&dev->slock, flags);
199 encoder_busy = dev->encoder_busy;
200 spin_unlock_irqrestore(&dev->slock, flags);
201
202 if (encoder_busy)
203 return;
204
205
206
207
208
209 for (i = 0; i < TW5864_INPUTS; i++) {
210 int next_input = (i + dev->next_input) % TW5864_INPUTS;
211 struct tw5864_input *input = &dev->inputs[next_input];
212 int raw_buf_id;
213
214 spin_lock_irqsave(&input->slock, flags);
215 if (!input->enabled)
216 goto next;
217
218
219 raw_buf_id = tw_mask_shift_readl(TW5864_SENIF_ORG_FRM_PTR1, 0x3,
220 2 * input->nr);
221
222 if (input->buf_id != raw_buf_id) {
223 input->buf_id = raw_buf_id;
224 tw5864_input_deadline_update(input);
225 spin_unlock_irqrestore(&input->slock, flags);
226
227 spin_lock_irqsave(&dev->slock, flags);
228 dev->encoder_busy = 1;
229 dev->next_input = (next_input + 1) % TW5864_INPUTS;
230 spin_unlock_irqrestore(&dev->slock, flags);
231
232 tw5864_request_encoded_frame(input);
233 break;
234 }
235
236
237 if (time_is_after_jiffies(input->new_frame_deadline)) {
238
239 tw_mask_shift_writel(TW5864_ENC_BUF_PTR_REC1, 0x3,
240 2 * input->nr, input->buf_id + 3);
241 tw5864_input_deadline_update(input);
242 }
243next:
244 spin_unlock_irqrestore(&input->slock, flags);
245 }
246}
247
248static int tw5864_initdev(struct pci_dev *pci_dev,
249 const struct pci_device_id *pci_id)
250{
251 struct tw5864_dev *dev;
252 int err;
253
254 dev = devm_kzalloc(&pci_dev->dev, sizeof(*dev), GFP_KERNEL);
255 if (!dev)
256 return -ENOMEM;
257
258 snprintf(dev->name, sizeof(dev->name), "tw5864:%s", pci_name(pci_dev));
259
260 err = v4l2_device_register(&pci_dev->dev, &dev->v4l2_dev);
261 if (err)
262 return err;
263
264
265 dev->pci = pci_dev;
266 err = pci_enable_device(pci_dev);
267 if (err) {
268 dev_err(&dev->pci->dev, "pci_enable_device() failed\n");
269 goto unreg_v4l2;
270 }
271
272 pci_set_master(pci_dev);
273
274 err = pci_set_dma_mask(pci_dev, DMA_BIT_MASK(32));
275 if (err) {
276 dev_err(&dev->pci->dev, "32 bit PCI DMA is not supported\n");
277 goto disable_pci;
278 }
279
280
281 err = pci_request_regions(pci_dev, dev->name);
282 if (err) {
283 dev_err(&dev->pci->dev, "Cannot request regions for MMIO\n");
284 goto disable_pci;
285 }
286 dev->mmio = pci_ioremap_bar(pci_dev, 0);
287 if (!dev->mmio) {
288 err = -EIO;
289 dev_err(&dev->pci->dev, "can't ioremap() MMIO memory\n");
290 goto release_mmio;
291 }
292
293 spin_lock_init(&dev->slock);
294
295 dev_info(&pci_dev->dev, "TW5864 hardware version: %04x\n",
296 tw_readl(TW5864_HW_VERSION));
297 dev_info(&pci_dev->dev, "TW5864 H.264 core version: %04x:%04x\n",
298 tw_readl(TW5864_H264REV),
299 tw_readl(TW5864_UNDECLARED_H264REV_PART2));
300
301 err = tw5864_video_init(dev, video_nr);
302 if (err)
303 goto unmap_mmio;
304
305
306 err = devm_request_irq(&pci_dev->dev, pci_dev->irq, tw5864_isr,
307 IRQF_SHARED, "tw5864", dev);
308 if (err < 0) {
309 dev_err(&dev->pci->dev, "can't get IRQ %d\n", pci_dev->irq);
310 goto fini_video;
311 }
312
313 dev_info(&pci_dev->dev, "Note: there are known video quality issues. For details\n");
314 dev_info(&pci_dev->dev, "see the comment in drivers/media/pci/tw5864/tw5864-core.c.\n");
315
316 return 0;
317
318fini_video:
319 tw5864_video_fini(dev);
320unmap_mmio:
321 iounmap(dev->mmio);
322release_mmio:
323 pci_release_regions(pci_dev);
324disable_pci:
325 pci_disable_device(pci_dev);
326unreg_v4l2:
327 v4l2_device_unregister(&dev->v4l2_dev);
328 return err;
329}
330
331static void tw5864_finidev(struct pci_dev *pci_dev)
332{
333 struct v4l2_device *v4l2_dev = pci_get_drvdata(pci_dev);
334 struct tw5864_dev *dev =
335 container_of(v4l2_dev, struct tw5864_dev, v4l2_dev);
336
337
338 tw5864_interrupts_disable(dev);
339
340
341 tw5864_video_fini(dev);
342
343
344 iounmap(dev->mmio);
345 release_mem_region(pci_resource_start(pci_dev, 0),
346 pci_resource_len(pci_dev, 0));
347
348 v4l2_device_unregister(&dev->v4l2_dev);
349 devm_kfree(&pci_dev->dev, dev);
350}
351
352static struct pci_driver tw5864_pci_driver = {
353 .name = "tw5864",
354 .id_table = tw5864_pci_tbl,
355 .probe = tw5864_initdev,
356 .remove = tw5864_finidev,
357};
358
359module_pci_driver(tw5864_pci_driver);
360