xref: /linux/drivers/media/pci/tw5864/tw5864-core.c (revision ab93e0dd72c37d378dd936f031ffb83ff2bd87ce) !
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *  TW5864 driver - core functions
4  *
5  *  Copyright (C) 2016 Bluecherry, LLC <maintainers@bluecherrydvr.com>
6  */
7 
8 #include <linux/init.h>
9 #include <linux/list.h>
10 #include <linux/module.h>
11 #include <linux/kernel.h>
12 #include <linux/slab.h>
13 #include <linux/kmod.h>
14 #include <linux/sound.h>
15 #include <linux/interrupt.h>
16 #include <linux/delay.h>
17 #include <linux/dma-mapping.h>
18 #include <linux/pm.h>
19 #include <linux/pci_ids.h>
20 #include <linux/jiffies.h>
21 #include <asm/dma.h>
22 #include <media/v4l2-dev.h>
23 
24 #include "tw5864.h"
25 #include "tw5864-reg.h"
26 
27 #define DRIVER_NAME "tw5864"
28 
29 MODULE_DESCRIPTION("V4L2 driver module for tw5864-based multimedia capture & encoding devices");
30 MODULE_AUTHOR("Bluecherry Maintainers <maintainers@bluecherrydvr.com>");
31 MODULE_AUTHOR("Andrey Utkin <andrey.utkin@corp.bluecherry.net>");
32 MODULE_LICENSE("GPL");
33 
34 /*
35  * BEWARE OF KNOWN ISSUES WITH VIDEO QUALITY
36  *
37  * This driver was developed by Bluecherry LLC by deducing behaviour of
38  * original manufacturer's driver, from both source code and execution traces.
39  * It is known that there are some artifacts on output video with this driver:
40  *  - on all known hardware samples: random pixels of wrong color (mostly
41  *    white, red or blue) appearing and disappearing on sequences of P-frames;
42  *  - on some hardware samples (known with H.264 core version e006:2800):
43  *    total madness on P-frames: blocks of wrong luminance; blocks of wrong
44  *    colors "creeping" across the picture.
45  * There is a workaround for both issues: avoid P-frames by setting GOP size
46  * to 1. To do that, run this command on device files created by this driver:
47  *
48  * v4l2-ctl --device /dev/videoX --set-ctrl=video_gop_size=1
49  *
50  * These issues are not decoding errors; all produced H.264 streams are decoded
51  * properly. Streams without P-frames don't have these artifacts so it's not
52  * analog-to-digital conversion issues nor internal memory errors; we conclude
53  * it's internal H.264 encoder issues.
54  * We cannot even check the original driver's behaviour because it has never
55  * worked properly at all in our development environment. So these issues may
56  * be actually related to firmware or hardware. However it may be that there's
57  * just some more register settings missing in the driver which would please
58  * the hardware.
59  * Manufacturer didn't help much on our inquiries, but feel free to disturb
60  * again the support of Intersil (owner of former Techwell).
61  */
62 
63 /* take first free /dev/videoX indexes by default */
64 static unsigned int video_nr[] = {[0 ... (TW5864_INPUTS - 1)] = -1 };
65 
66 module_param_array(video_nr, int, NULL, 0444);
67 MODULE_PARM_DESC(video_nr, "video devices numbers array");
68 
69 /*
70  * Please add any new PCI IDs to: https://pci-ids.ucw.cz.  This keeps
71  * the PCI ID database up to date.  Note that the entries must be
72  * added under vendor 0x1797 (Techwell Inc.) as subsystem IDs.
73  */
74 static const struct pci_device_id tw5864_pci_tbl[] = {
75 	{PCI_DEVICE(PCI_VENDOR_ID_TECHWELL, PCI_DEVICE_ID_TECHWELL_5864)},
76 	{0,}
77 };
78 
tw5864_irqmask_apply(struct tw5864_dev * dev)79 void tw5864_irqmask_apply(struct tw5864_dev *dev)
80 {
81 	tw_writel(TW5864_INTR_ENABLE_L, dev->irqmask & 0xffff);
82 	tw_writel(TW5864_INTR_ENABLE_H, (dev->irqmask >> 16));
83 }
84 
tw5864_interrupts_disable(struct tw5864_dev * dev)85 static void tw5864_interrupts_disable(struct tw5864_dev *dev)
86 {
87 	unsigned long flags;
88 
89 	spin_lock_irqsave(&dev->slock, flags);
90 	dev->irqmask = 0;
91 	tw5864_irqmask_apply(dev);
92 	spin_unlock_irqrestore(&dev->slock, flags);
93 }
94 
95 static void tw5864_timer_isr(struct tw5864_dev *dev);
96 static void tw5864_h264_isr(struct tw5864_dev *dev);
97 
tw5864_isr(int irq,void * dev_id)98 static irqreturn_t tw5864_isr(int irq, void *dev_id)
99 {
100 	struct tw5864_dev *dev = dev_id;
101 	u32 status;
102 
103 	status = tw_readl(TW5864_INTR_STATUS_L) |
104 		tw_readl(TW5864_INTR_STATUS_H) << 16;
105 	if (!status)
106 		return IRQ_NONE;
107 
108 	tw_writel(TW5864_INTR_CLR_L, 0xffff);
109 	tw_writel(TW5864_INTR_CLR_H, 0xffff);
110 
111 	if (status & TW5864_INTR_VLC_DONE)
112 		tw5864_h264_isr(dev);
113 
114 	if (status & TW5864_INTR_TIMER)
115 		tw5864_timer_isr(dev);
116 
117 	if (!(status & (TW5864_INTR_TIMER | TW5864_INTR_VLC_DONE))) {
118 		dev_dbg(&dev->pci->dev, "Unknown interrupt, status 0x%08X\n",
119 			status);
120 	}
121 
122 	return IRQ_HANDLED;
123 }
124 
tw5864_h264_isr(struct tw5864_dev * dev)125 static void tw5864_h264_isr(struct tw5864_dev *dev)
126 {
127 	int channel = tw_readl(TW5864_DSP) & TW5864_DSP_ENC_CHN;
128 	struct tw5864_input *input = &dev->inputs[channel];
129 	int cur_frame_index, next_frame_index;
130 	struct tw5864_h264_frame *cur_frame, *next_frame;
131 	unsigned long flags;
132 
133 	spin_lock_irqsave(&dev->slock, flags);
134 
135 	cur_frame_index = dev->h264_buf_w_index;
136 	next_frame_index = (cur_frame_index + 1) % H264_BUF_CNT;
137 	cur_frame = &dev->h264_buf[cur_frame_index];
138 	next_frame = &dev->h264_buf[next_frame_index];
139 
140 	if (next_frame_index != dev->h264_buf_r_index) {
141 		cur_frame->vlc_len = tw_readl(TW5864_VLC_LENGTH) << 2;
142 		cur_frame->checksum = tw_readl(TW5864_VLC_CRC_REG);
143 		cur_frame->input = input;
144 		cur_frame->timestamp = ktime_get_ns();
145 		cur_frame->seqno = input->frame_seqno;
146 		cur_frame->gop_seqno = input->frame_gop_seqno;
147 
148 		dev->h264_buf_w_index = next_frame_index;
149 		queue_work(system_bh_wq, &dev->bh_work);
150 
151 		cur_frame = next_frame;
152 
153 		spin_lock(&input->slock);
154 		input->frame_seqno++;
155 		input->frame_gop_seqno++;
156 		if (input->frame_gop_seqno >= input->gop)
157 			input->frame_gop_seqno = 0;
158 		spin_unlock(&input->slock);
159 	} else {
160 		dev_err(&dev->pci->dev,
161 			"Skipped frame on input %d because all buffers busy\n",
162 			channel);
163 	}
164 
165 	dev->encoder_busy = 0;
166 
167 	spin_unlock_irqrestore(&dev->slock, flags);
168 
169 	tw_writel(TW5864_VLC_STREAM_BASE_ADDR, cur_frame->vlc.dma_addr);
170 	tw_writel(TW5864_MV_STREAM_BASE_ADDR, cur_frame->mv.dma_addr);
171 
172 	/* Additional ack for this interrupt */
173 	tw_writel(TW5864_VLC_DSP_INTR, 0x00000001);
174 	tw_writel(TW5864_PCI_INTR_STATUS, TW5864_VLC_DONE_INTR);
175 }
176 
tw5864_input_deadline_update(struct tw5864_input * input)177 static void tw5864_input_deadline_update(struct tw5864_input *input)
178 {
179 	input->new_frame_deadline = jiffies + msecs_to_jiffies(1000);
180 }
181 
tw5864_timer_isr(struct tw5864_dev * dev)182 static void tw5864_timer_isr(struct tw5864_dev *dev)
183 {
184 	unsigned long flags;
185 	int i;
186 	int encoder_busy;
187 
188 	/* Additional ack for this interrupt */
189 	tw_writel(TW5864_PCI_INTR_STATUS, TW5864_TIMER_INTR);
190 
191 	spin_lock_irqsave(&dev->slock, flags);
192 	encoder_busy = dev->encoder_busy;
193 	spin_unlock_irqrestore(&dev->slock, flags);
194 
195 	if (encoder_busy)
196 		return;
197 
198 	/*
199 	 * Traversing inputs in round-robin fashion, starting from next to the
200 	 * last processed one
201 	 */
202 	for (i = 0; i < TW5864_INPUTS; i++) {
203 		int next_input = (i + dev->next_input) % TW5864_INPUTS;
204 		struct tw5864_input *input = &dev->inputs[next_input];
205 		int raw_buf_id; /* id of internal buf with last raw frame */
206 
207 		spin_lock_irqsave(&input->slock, flags);
208 		if (!input->enabled)
209 			goto next;
210 
211 		/* Check if new raw frame is available */
212 		raw_buf_id = tw_mask_shift_readl(TW5864_SENIF_ORG_FRM_PTR1, 0x3,
213 						 2 * input->nr);
214 
215 		if (input->buf_id != raw_buf_id) {
216 			input->buf_id = raw_buf_id;
217 			tw5864_input_deadline_update(input);
218 			spin_unlock_irqrestore(&input->slock, flags);
219 
220 			spin_lock_irqsave(&dev->slock, flags);
221 			dev->encoder_busy = 1;
222 			dev->next_input = (next_input + 1) % TW5864_INPUTS;
223 			spin_unlock_irqrestore(&dev->slock, flags);
224 
225 			tw5864_request_encoded_frame(input);
226 			break;
227 		}
228 
229 		/* No new raw frame; check if channel is stuck */
230 		if (time_is_after_jiffies(input->new_frame_deadline)) {
231 			/* If stuck, request new raw frames again */
232 			tw_mask_shift_writel(TW5864_ENC_BUF_PTR_REC1, 0x3,
233 					     2 * input->nr, input->buf_id + 3);
234 			tw5864_input_deadline_update(input);
235 		}
236 next:
237 		spin_unlock_irqrestore(&input->slock, flags);
238 	}
239 }
240 
tw5864_initdev(struct pci_dev * pci_dev,const struct pci_device_id * pci_id)241 static int tw5864_initdev(struct pci_dev *pci_dev,
242 			  const struct pci_device_id *pci_id)
243 {
244 	struct tw5864_dev *dev;
245 	int err;
246 
247 	dev = devm_kzalloc(&pci_dev->dev, sizeof(*dev), GFP_KERNEL);
248 	if (!dev)
249 		return -ENOMEM;
250 
251 	snprintf(dev->name, sizeof(dev->name), "%s:%s", DRIVER_NAME,
252 		 pci_name(pci_dev));
253 
254 	err = v4l2_device_register(&pci_dev->dev, &dev->v4l2_dev);
255 	if (err)
256 		return err;
257 
258 	/* pci init */
259 	dev->pci = pci_dev;
260 	err = pcim_enable_device(pci_dev);
261 	if (err) {
262 		dev_err(&dev->pci->dev, "pcim_enable_device() failed\n");
263 		goto unreg_v4l2;
264 	}
265 
266 	pci_set_master(pci_dev);
267 
268 	err = dma_set_mask(&pci_dev->dev, DMA_BIT_MASK(32));
269 	if (err) {
270 		dev_err(&dev->pci->dev, "32 bit PCI DMA is not supported\n");
271 		goto unreg_v4l2;
272 	}
273 
274 	/* get mmio */
275 	dev->mmio = pcim_iomap_region(pci_dev, 0, DRIVER_NAME);
276 	err = PTR_ERR_OR_ZERO(dev->mmio);
277 	if (err) {
278 		dev_err(&dev->pci->dev, "Cannot request regions for MMIO\n");
279 		goto unreg_v4l2;
280 	}
281 
282 	spin_lock_init(&dev->slock);
283 
284 	dev_info(&pci_dev->dev, "TW5864 hardware version: %04x\n",
285 		 tw_readl(TW5864_HW_VERSION));
286 	dev_info(&pci_dev->dev, "TW5864 H.264 core version: %04x:%04x\n",
287 		 tw_readl(TW5864_H264REV),
288 		 tw_readl(TW5864_UNDECLARED_H264REV_PART2));
289 
290 	err = tw5864_video_init(dev, video_nr);
291 	if (err)
292 		goto unreg_v4l2;
293 
294 	/* get irq */
295 	err = devm_request_irq(&pci_dev->dev, pci_dev->irq, tw5864_isr,
296 			       IRQF_SHARED, DRIVER_NAME, dev);
297 	if (err < 0) {
298 		dev_err(&dev->pci->dev, "can't get IRQ %d\n", pci_dev->irq);
299 		goto fini_video;
300 	}
301 
302 	dev_info(&pci_dev->dev, "Note: there are known video quality issues. For details\n");
303 	dev_info(&pci_dev->dev, "see the comment in drivers/media/pci/tw5864/tw5864-core.c.\n");
304 
305 	return 0;
306 
307 fini_video:
308 	tw5864_video_fini(dev);
309 unreg_v4l2:
310 	v4l2_device_unregister(&dev->v4l2_dev);
311 	return err;
312 }
313 
tw5864_finidev(struct pci_dev * pci_dev)314 static void tw5864_finidev(struct pci_dev *pci_dev)
315 {
316 	struct v4l2_device *v4l2_dev = pci_get_drvdata(pci_dev);
317 	struct tw5864_dev *dev =
318 		container_of(v4l2_dev, struct tw5864_dev, v4l2_dev);
319 
320 	/* shutdown subsystems */
321 	tw5864_interrupts_disable(dev);
322 
323 	/* unregister */
324 	tw5864_video_fini(dev);
325 
326 	v4l2_device_unregister(&dev->v4l2_dev);
327 }
328 
329 static struct pci_driver tw5864_pci_driver = {
330 	.name = DRIVER_NAME,
331 	.id_table = tw5864_pci_tbl,
332 	.probe = tw5864_initdev,
333 	.remove = tw5864_finidev,
334 };
335 
336 module_pci_driver(tw5864_pci_driver);
337