summaryrefslogtreecommitdiffstats
path: root/drivers/media/pci/saa7164/saa7164.h
blob: 2801a2b03fa0963cb5e40896adb129d8fde69908 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
/* SPDX-License-Identifier: GPL-2.0-or-later */
/*
 *  Driver for the NXP SAA7164 PCIe bridge
 *
 *  Copyright (c) 2010-2015 Steven Toth <stoth@kernellabs.com>
 */

/*
	Driver architecture
	*******************

	saa7164_core.c/buffer.c/cards.c/i2c.c/dvb.c
		|	: Standard Linux driver framework for creating
		|	: exposing and managing interfaces to the rest
		|	: of the kernel or userland. Also uses _fw.c to load
		|	: firmware direct into the PCIe bus, bypassing layers.
		V
	saa7164_api..()	: Translate kernel specific functions/features
		|	: into command buffers.
		V
	saa7164_cmd..()	: Manages the flow of command packets on/off,
		|	: the bus. Deal with bus errors, timeouts etc.
		V
	saa7164_bus..() : Manage a read/write memory ring buffer in the
		|	: PCIe Address space.
		|
		|		saa7164_fw...()	: Load any frimware
		|			|	: direct into the device
		V			V
	<- ----------------- PCIe address space -------------------- ->
*/

#include <linux/pci.h>
#include <linux/i2c.h>
#include <linux/kdev_t.h>
#include <linux/mutex.h>
#include <linux/crc32.h>
#include <linux/kthread.h>
#include <linux/freezer.h>

#include <media/tuner.h>
#include <media/tveeprom.h>
#include <media/dvb_demux.h>
#include <media/dvb_frontend.h>
#include <media/dvb_net.h>
#include <media/dvbdev.h>
#include <media/dmxdev.h>
#include <media/v4l2-common.h>
#include <media/v4l2-ioctl.h>
#include <media/v4l2-device.h>
#include <media/v4l2-ctrls.h>
#include <media/v4l2-event.h>

#include "saa7164-reg.h"
#include "saa7164-types.h"

#define SAA7164_MAXBOARDS 8

#define UNSET (-1U)
#define SAA7164_BOARD_NOAUTO			UNSET
#define SAA7164_BOARD_UNKNOWN			0
#define SAA7164_BOARD_UNKNOWN_REV2		1
#define SAA7164_BOARD_UNKNOWN_REV3		2
#define SAA7164_BOARD_HAUPPAUGE_HVR2250		3
#define SAA7164_BOARD_HAUPPAUGE_HVR2200		4
#define SAA7164_BOARD_HAUPPAUGE_HVR2200_2	5
#define SAA7164_BOARD_HAUPPAUGE_HVR2200_3	6
#define SAA7164_BOARD_HAUPPAUGE_HVR2250_2	7
#define SAA7164_BOARD_HAUPPAUGE_HVR2250_3	8
#define SAA7164_BOARD_HAUPPAUGE_HVR2200_4	9
#define SAA7164_BOARD_HAUPPAUGE_HVR2200_5	10
#define SAA7164_BOARD_HAUPPAUGE_HVR2255proto	11
#define SAA7164_BOARD_HAUPPAUGE_HVR2255		12
#define SAA7164_BOARD_HAUPPAUGE_HVR2205		13

#define SAA7164_MAX_UNITS		8
#define SAA7164_TS_NUMBER_OF_LINES	312
#define SAA7164_PS_NUMBER_OF_LINES	256
#define SAA7164_PT_ENTRIES		16 /* (312 * 188) / 4096 */
#define SAA7164_MAX_ENCODER_BUFFERS	64 /* max 5secs of latency at 6Mbps */
#define SAA7164_MAX_VBI_BUFFERS		64

/* Port related defines */
#define SAA7164_PORT_TS1	(0)
#define SAA7164_PORT_TS2	(SAA7164_PORT_TS1 + 1)
#define SAA7164_PORT_ENC1	(SAA7164_PORT_TS2 + 1)
#define SAA7164_PORT_ENC2	(SAA7164_PORT_ENC1 + 1)
#define SAA7164_PORT_VBI1	(SAA7164_PORT_ENC2 + 1)
#define SAA7164_PORT_VBI2	(SAA7164_PORT_VBI1 + 1)
#define SAA7164_MAX_PORTS	(SAA7164_PORT_VBI2 + 1)

#define DBGLVL_FW    4
#define DBGLVL_DVB   8
#define DBGLVL_I2C  16
#define DBGLVL_API  32
#define DBGLVL_CMD  64
#define DBGLVL_BUS 128
#define DBGLVL_IRQ 256
#define DBGLVL_BUF 512
#define DBGLVL_ENC 1024
#define DBGLVL_VBI 2048
#define DBGLVL_THR 4096
#define DBGLVL_CPU 8192

#define SAA7164_NORMS \
	(V4L2_STD_NTSC_M | V4L2_STD_NTSC_M_JP)

/* TV frequency range copied from tuner-core.c */
#define SAA7164_TV_MIN_FREQ (44U * 16U)
#define SAA7164_TV_MAX_FREQ (958U * 16U)

enum port_t {
	SAA7164_MPEG_UNDEFINED = 0,
	SAA7164_MPEG_DVB,
	SAA7164_MPEG_ENCODER,
	SAA7164_MPEG_VBI,
};

enum saa7164_i2c_bus_nr {
	SAA7164_I2C_BUS_0 = 0,
	SAA7164_I2C_BUS_1,
	SAA7164_I2C_BUS_2,
};

enum saa7164_buffer_flags {
	SAA7164_BUFFER_UNDEFINED = 0,
	SAA7164_BUFFER_FREE,
	SAA7164_BUFFER_BUSY,
	SAA7164_BUFFER_FULL
};

enum saa7164_unit_type {
	SAA7164_UNIT_UNDEFINED = 0,
	SAA7164_UNIT_DIGITAL_DEMODULATOR,
	SAA7164_UNIT_ANALOG_DEMODULATOR,
	SAA7164_UNIT_TUNER,
	SAA7164_UNIT_EEPROM,
	SAA7164_UNIT_ZILOG_IRBLASTER,
	SAA7164_UNIT_ENCODER,
};

/* The PCIe bridge doesn't grant direct access to i2c.
 * Instead, you address i2c devices using a uniqely
 * allocated 'unitid' value via a messaging API. This
 * is a problem. The kernel and existing demod/tuner
 * drivers expect to talk 'i2c', so we have to maintain
 * a translation layer, and a series of functions to
 * convert i2c bus + device address into a unit id.
 */
struct saa7164_unit {
	enum saa7164_unit_type type;
	u8	id;
	char	*name;
	enum saa7164_i2c_bus_nr i2c_bus_nr;
	u8	i2c_bus_addr;
	u8	i2c_reg_len;
};

struct saa7164_board {
	char	*name;
	enum port_t porta, portb, portc,
		portd, porte, portf;
	enum {
		SAA7164_CHIP_UNDEFINED = 0,
		SAA7164_CHIP_REV2,
		SAA7164_CHIP_REV3,
	} chiprev;
	struct	saa7164_unit unit[SAA7164_MAX_UNITS];
};

struct saa7164_subid {
	u16     subvendor;
	u16     subdevice;
	u32     card;
};

struct saa7164_encoder_fh {
	struct v4l2_fh fh;
	struct saa7164_port *port;
	atomic_t v4l_reading;
};

struct saa7164_vbi_fh {
	struct v4l2_fh fh;
	struct saa7164_port *port;
	atomic_t v4l_reading;
};

struct saa7164_histogram_bucket {
	u32 val;
	u32 count;
	u64 update_time;
};

struct saa7164_histogram {
	char name[32];
	struct saa7164_histogram_bucket counter1[64];
};

struct saa7164_user_buffer {
	struct list_head list;

	/* Attributes */
	u8  *data;
	u32 pos;
	u32 actual_size;

	u32 crc;
};

struct saa7164_fw_status {

	/* RISC Core details */
	u32	status;
	u32	mode;
	u32	spec;
	u32	inst;
	u32	cpuload;
	u32	remainheap;

	/* Firmware version */
	u32	version;
	u32	major;
	u32	sub;
	u32	rel;
	u32	buildnr;
};

struct saa7164_dvb {
	struct mutex lock;
	struct dvb_adapter adapter;
	struct dvb_frontend *frontend;
	struct dvb_demux demux;
	struct dmxdev dmxdev;
	struct dmx_frontend fe_hw;
	struct dmx_frontend fe_mem;
	struct dvb_net net;
	int feeding;
};

struct saa7164_i2c {
	struct saa7164_dev		*dev;

	enum saa7164_i2c_bus_nr		nr;

	/* I2C I/O */
	struct i2c_adapter		i2c_adap;
	struct i2c_client		i2c_client;
	u32				i2c_rc;
};

struct saa7164_tvnorm {
	char		*name;
	v4l2_std_id	id;
};

struct saa7164_encoder_params {
	struct saa7164_tvnorm encodernorm;
	u32 height;
	u32 width;
	u32 is_50hz;
	u32 bitrate; /* bps */
	u32 bitrate_peak; /* bps */
	u32 bitrate_mode;
	u32 stream_type; /* V4L2_MPEG_STREAM_TYPE_MPEG2_TS */

	u32 audio_sampling_freq;
	u32 ctl_mute;
	u32 ctl_aspect;
	u32 refdist;
	u32 gop_size;
};

struct saa7164_vbi_params {
	struct saa7164_tvnorm encodernorm;
	u32 height;
	u32 width;
	u32 is_50hz;
	u32 bitrate; /* bps */
	u32 bitrate_peak; /* bps */
	u32 bitrate_mode;
	u32 stream_type; /* V4L2_MPEG_STREAM_TYPE_MPEG2_TS */

	u32 audio_sampling_freq;
	u32 ctl_mute;
	u32 ctl_aspect;
	u32 refdist;
	u32 gop_size;
};

struct saa7164_port;

struct saa7164_buffer {
	struct list_head list;

	/* Note of which h/w buffer list index position we occupy */
	int idx;

	struct saa7164_port *port;

	/* Hardware Specific */
	/* PCI Memory allocations */
	enum saa7164_buffer_flags flags; /* Free, Busy, Full */

	/* A block of page align PCI memory */
	u32 pci_size;	/* PCI allocation size in bytes */
	u64 *cpu;	/* Virtual address */
	dma_addr_t dma;	/* Physical address */
	u32 crc;	/* Checksum for the entire buffer data */

	/* A page table that splits the block into a number of entries */
	u32 pt_size;		/* PCI allocation size in bytes */
	u64 *pt_cpu;		/* Virtual address */
	dma_addr_t pt_dma;	/* Physical address */

	/* Encoder fops */
	u32 pos;
	u32 actual_size;
};

struct saa7164_port {

	struct saa7164_dev *dev;
	enum port_t type;
	int nr;

	/* --- Generic port attributes --- */

	/* HW stream parameters */
	struct tmHWStreamParameters hw_streamingparams;

	/* DMA configuration values, is seeded during initialization */
	struct tmComResDMATermDescrHeader hwcfg;

	/* hardware specific registers */
	u32 bufcounter;
	u32 pitch;
	u32 bufsize;
	u32 bufoffset;
	u32 bufptr32l;
	u32 bufptr32h;
	u64 bufptr64;

	u32 numpte;	/* Number of entries in array, only valid in head */

	struct mutex dmaqueue_lock;
	struct saa7164_buffer dmaqueue;

	u64 last_irq_msecs, last_svc_msecs;
	u64 last_irq_msecs_diff, last_svc_msecs_diff;
	u32 last_svc_wp;
	u32 last_svc_rp;
	u64 last_irq_svc_msecs_diff;
	u64 last_read_msecs, last_read_msecs_diff;
	u64 last_poll_msecs, last_poll_msecs_diff;

	struct saa7164_histogram irq_interval;
	struct saa7164_histogram svc_interval;
	struct saa7164_histogram irq_svc_interval;
	struct saa7164_histogram read_interval;
	struct saa7164_histogram poll_interval;

	/* --- DVB Transport Specific --- */
	struct saa7164_dvb dvb;
	struct i2c_client