1 /* SPDX-License-Identifier: GPL-2.0-or-later */
3 * Driver for the NXP SAA7164 PCIe bridge
5 * Copyright (c) 2010-2015 Steven Toth <stoth@kernellabs.com>
12 saa7164_core.c/buffer.c/cards.c/i2c.c/dvb.c
13 | : Standard Linux driver framework for creating
14 | : exposing and managing interfaces to the rest
15 | : of the kernel or userland. Also uses _fw.c to load
16 | : firmware direct into the PCIe bus, bypassing layers.
18 saa7164_api..() : Translate kernel specific functions/features
19 | : into command buffers.
21 saa7164_cmd..() : Manages the flow of command packets on/off,
22 | : the bus. Deal with bus errors, timeouts etc.
24 saa7164_bus..() : Manage a read/write memory ring buffer in the
25 | : PCIe Address space.
27 | saa7164_fw...() : Load any frimware
28 | | : direct into the device
30 <- ----------------- PCIe address space -------------------- ->
33 #include <linux/pci.h>
34 #include <linux/i2c.h>
35 #include <linux/kdev_t.h>
36 #include <linux/mutex.h>
37 #include <linux/crc32.h>
38 #include <linux/kthread.h>
39 #include <linux/freezer.h>
41 #include <media/tuner.h>
42 #include <media/tveeprom.h>
43 #include <media/dvb_demux.h>
44 #include <media/dvb_frontend.h>
45 #include <media/dvb_net.h>
46 #include <media/dvbdev.h>
47 #include <media/dmxdev.h>
48 #include <media/v4l2-common.h>
49 #include <media/v4l2-ioctl.h>
50 #include <media/v4l2-device.h>
51 #include <media/v4l2-ctrls.h>
52 #include <media/v4l2-event.h>
54 #include "saa7164-reg.h"
55 #include "saa7164-types.h"
57 #define SAA7164_MAXBOARDS 8
60 #define SAA7164_BOARD_NOAUTO UNSET
61 #define SAA7164_BOARD_UNKNOWN 0
62 #define SAA7164_BOARD_UNKNOWN_REV2 1
63 #define SAA7164_BOARD_UNKNOWN_REV3 2
64 #define SAA7164_BOARD_HAUPPAUGE_HVR2250 3
65 #define SAA7164_BOARD_HAUPPAUGE_HVR2200 4
66 #define SAA7164_BOARD_HAUPPAUGE_HVR2200_2 5
67 #define SAA7164_BOARD_HAUPPAUGE_HVR2200_3 6
68 #define SAA7164_BOARD_HAUPPAUGE_HVR2250_2 7
69 #define SAA7164_BOARD_HAUPPAUGE_HVR2250_3 8
70 #define SAA7164_BOARD_HAUPPAUGE_HVR2200_4 9
71 #define SAA7164_BOARD_HAUPPAUGE_HVR2200_5 10
72 #define SAA7164_BOARD_HAUPPAUGE_HVR2255proto 11
73 #define SAA7164_BOARD_HAUPPAUGE_HVR2255 12
74 #define SAA7164_BOARD_HAUPPAUGE_HVR2205 13
76 #define SAA7164_MAX_UNITS 8
77 #define SAA7164_TS_NUMBER_OF_LINES 312
78 #define SAA7164_PS_NUMBER_OF_LINES 256
79 #define SAA7164_PT_ENTRIES 16 /* (312 * 188) / 4096 */
80 #define SAA7164_MAX_ENCODER_BUFFERS 64 /* max 5secs of latency at 6Mbps */
81 #define SAA7164_MAX_VBI_BUFFERS 64
83 /* Port related defines */
84 #define SAA7164_PORT_TS1 (0)
85 #define SAA7164_PORT_TS2 (SAA7164_PORT_TS1 + 1)
86 #define SAA7164_PORT_ENC1 (SAA7164_PORT_TS2 + 1)
87 #define SAA7164_PORT_ENC2 (SAA7164_PORT_ENC1 + 1)
88 #define SAA7164_PORT_VBI1 (SAA7164_PORT_ENC2 + 1)
89 #define SAA7164_PORT_VBI2 (SAA7164_PORT_VBI1 + 1)
90 #define SAA7164_MAX_PORTS (SAA7164_PORT_VBI2 + 1)
97 #define DBGLVL_BUS 128
98 #define DBGLVL_IRQ 256
99 #define DBGLVL_BUF 512
100 #define DBGLVL_ENC 1024
101 #define DBGLVL_VBI 2048
102 #define DBGLVL_THR 4096
103 #define DBGLVL_CPU 8192
105 #define SAA7164_NORMS \
106 (V4L2_STD_NTSC_M | V4L2_STD_NTSC_M_JP)
108 /* TV frequency range copied from tuner-core.c */
109 #define SAA7164_TV_MIN_FREQ (44U * 16U)
110 #define SAA7164_TV_MAX_FREQ (958U * 16U)
113 SAA7164_MPEG_UNDEFINED
= 0,
115 SAA7164_MPEG_ENCODER
,
119 enum saa7164_i2c_bus_nr
{
120 SAA7164_I2C_BUS_0
= 0,
125 enum saa7164_buffer_flags
{
126 SAA7164_BUFFER_UNDEFINED
= 0,
132 enum saa7164_unit_type
{
133 SAA7164_UNIT_UNDEFINED
= 0,
134 SAA7164_UNIT_DIGITAL_DEMODULATOR
,
135 SAA7164_UNIT_ANALOG_DEMODULATOR
,
138 SAA7164_UNIT_ZILOG_IRBLASTER
,
139 SAA7164_UNIT_ENCODER
,
142 /* The PCIe bridge doesn't grant direct access to i2c.
143 * Instead, you address i2c devices using a uniqely
144 * allocated 'unitid' value via a messaging API. This
145 * is a problem. The kernel and existing demod/tuner
146 * drivers expect to talk 'i2c', so we have to maintain
147 * a translation layer, and a series of functions to
148 * convert i2c bus + device address into a unit id.
150 struct saa7164_unit
{
151 enum saa7164_unit_type type
;
154 enum saa7164_i2c_bus_nr i2c_bus_nr
;
159 struct saa7164_board
{
161 enum port_t porta
, portb
, portc
,
164 SAA7164_CHIP_UNDEFINED
= 0,
168 struct saa7164_unit unit
[SAA7164_MAX_UNITS
];
171 struct saa7164_subid
{
177 struct saa7164_encoder_fh
{
179 struct saa7164_port
*port
;
180 atomic_t v4l_reading
;
183 struct saa7164_vbi_fh
{
185 struct saa7164_port
*port
;
186 atomic_t v4l_reading
;
189 struct saa7164_histogram_bucket
{
195 struct saa7164_histogram
{
197 struct saa7164_histogram_bucket counter1
[64];
200 struct saa7164_user_buffer
{
201 struct list_head list
;
211 struct saa7164_fw_status
{
213 /* RISC Core details */
221 /* Firmware version */
231 struct dvb_adapter adapter
;
232 struct dvb_frontend
*frontend
;
233 struct dvb_demux demux
;
234 struct dmxdev dmxdev
;
235 struct dmx_frontend fe_hw
;
236 struct dmx_frontend fe_mem
;
242 struct saa7164_dev
*dev
;
244 enum saa7164_i2c_bus_nr nr
;
247 struct i2c_adapter i2c_adap
;
248 struct i2c_client i2c_client
;
252 struct saa7164_tvnorm
{
257 struct saa7164_encoder_params
{
258 struct saa7164_tvnorm encodernorm
;
262 u32 bitrate
; /* bps */
263 u32 bitrate_peak
; /* bps */
265 u32 stream_type
; /* V4L2_MPEG_STREAM_TYPE_MPEG2_TS */
267 u32 audio_sampling_freq
;
274 struct saa7164_vbi_params
{
275 struct saa7164_tvnorm encodernorm
;
279 u32 bitrate
; /* bps */
280 u32 bitrate_peak
; /* bps */
282 u32 stream_type
; /* V4L2_MPEG_STREAM_TYPE_MPEG2_TS */
284 u32 audio_sampling_freq
;
293 struct saa7164_buffer
{
294 struct list_head list
;
296 /* Note of which h/w buffer list index position we occupy */
299 struct saa7164_port
*port
;
301 /* Hardware Specific */
302 /* PCI Memory allocations */
303 enum saa7164_buffer_flags flags
; /* Free, Busy, Full */
305 /* A block of page align PCI memory */
306 u32 pci_size
; /* PCI allocation size in bytes */
307 u64
*cpu
; /* Virtual address */
308 dma_addr_t dma
; /* Physical address */
309 u32 crc
; /* Checksum for the entire buffer data */
311 /* A page table that splits the block into a number of entries */
312 u32 pt_size
; /* PCI allocation size in bytes */
313 u64
*pt_cpu
; /* Virtual address */
314 dma_addr_t pt_dma
; /* Physical address */
321 struct saa7164_port
{
323 struct saa7164_dev
*dev
;
327 /* --- Generic port attributes --- */
329 /* HW stream parameters */
330 struct tmHWStreamParameters hw_streamingparams
;
332 /* DMA configuration values, is seeded during initialization */
333 struct tmComResDMATermDescrHeader hwcfg
;
335 /* hardware specific registers */
344 u32 numpte
; /* Number of entries in array, only valid in head */
346 struct mutex dmaqueue_lock
;
347 struct saa7164_buffer dmaqueue
;
349 u64 last_irq_msecs
, last_svc_msecs
;
350 u64 last_irq_msecs_diff
, last_svc_msecs_diff
;
353 u64 last_irq_svc_msecs_diff
;
354 u64 last_read_msecs
, last_read_msecs_diff
;
355 u64 last_poll_msecs
, last_poll_msecs_diff
;
357 struct saa7164_histogram irq_interval
;
358 struct saa7164_histogram svc_interval
;
359 struct saa7164_histogram irq_svc_interval
;
360 struct saa7164_histogram read_interval
;
361 struct saa7164_histogram poll_interval
;
363 /* --- DVB Transport Specific --- */
364 struct saa7164_dvb dvb
;
365 struct i2c_client
*i2c_client_demod
;
366 struct i2c_client
*i2c_client_tuner
;
368 /* --- Encoder/V4L related attributes --- */
370 /* Defaults established in saa7164-encoder.c */
371 struct saa7164_tvnorm encodernorm
;
372 struct v4l2_ctrl_handler ctrl_handler
;
389 struct tmComResAFeatureDescrHeader audfeat
;
390 struct tmComResEncoderDescrHeader encunit
;
391 struct tmComResProcDescrHeader vidproc
;
392 struct tmComResExtDevDescrHeader ifunit
;
393 struct tmComResTunerDescrHeader tunerunit
;
395 struct work_struct workenc
;
397 /* V4L Encoder Video */
398 struct saa7164_encoder_params encoder_params
;
399 struct video_device
*v4l_device
;
400 atomic_t v4l_reader_count
;
402 struct saa7164_buffer list_buf_used
;
403 struct saa7164_buffer list_buf_free
;
404 wait_queue_head_t wait_read
;
407 struct tmComResVBIFormatDescrHeader vbi_fmt_ntsc
;
408 struct saa7164_vbi_params vbi_params
;
409 struct saa7164_port
*enc_port
;
417 u32 done_first_interrupt
;
421 struct list_head devlist
;
424 struct v4l2_device v4l2_dev
;
428 unsigned char pci_rev
, pci_lat
;
429 int pci_bus
, pci_slot
;
442 /* firmware status */
443 struct saa7164_fw_status fw_status
;
446 struct tmComResHWDescr hwdesc
;
447 struct tmComResInterfaceDescr intfdesc
;
448 struct tmComResBusDescr busdesc
;
450 struct tmComResBusInfo bus
;
452 /* Interrupt status and ack registers */
457 struct cmd cmds
[SAA_CMD_MAX_MSG_UNITS
];
461 struct saa7164_i2c i2c_bus
[3];
463 /* Transport related */
464 struct saa7164_port ports
[SAA7164_MAX_PORTS
];
466 /* Deferred command/api interrupts handling */
467 struct work_struct workcmd
;
469 /* A kernel thread to monitor the firmware log, used
470 * only in debug mode.
472 struct task_struct
*kthread
;
476 extern struct list_head saa7164_devlist
;
477 extern unsigned int waitsecs
;
478 extern unsigned int encoder_buffers
;
479 extern unsigned int vbi_buffers
;
481 /* ----------------------------------------------------------- */
483 void saa7164_dumpregs(struct saa7164_dev
*dev
, u32 addr
);
484 void saa7164_getfirmwarestatus(struct saa7164_dev
*dev
);
485 u32
saa7164_getcurrentfirmwareversion(struct saa7164_dev
*dev
);
486 void saa7164_histogram_update(struct saa7164_histogram
*hg
, u32 val
);
488 /* ----------------------------------------------------------- */
490 int saa7164_downloadfirmware(struct saa7164_dev
*dev
);
492 /* ----------------------------------------------------------- */
494 extern int saa7164_i2c_register(struct saa7164_i2c
*bus
);
495 extern int saa7164_i2c_unregister(struct saa7164_i2c
*bus
);
496 extern void saa7164_call_i2c_clients(struct saa7164_i2c
*bus
,
497 unsigned int cmd
, void *arg
);
499 /* ----------------------------------------------------------- */
501 int saa7164_bus_setup(struct saa7164_dev
*dev
);
502 void saa7164_bus_dump(struct saa7164_dev
*dev
);
503 int saa7164_bus_set(struct saa7164_dev
*dev
, struct tmComResInfo
* msg
,
505 int saa7164_bus_get(struct saa7164_dev
*dev
, struct tmComResInfo
* msg
,
506 void *buf
, int peekonly
);
508 /* ----------------------------------------------------------- */
510 int saa7164_cmd_send(struct saa7164_dev
*dev
,
511 u8 id
, enum tmComResCmd command
, u16 controlselector
,
512 u16 size
, void *buf
);
513 void saa7164_cmd_signal(struct saa7164_dev
*dev
, u8 seqno
);
514 int saa7164_irq_dequeue(struct saa7164_dev
*dev
);
516 /* ----------------------------------------------------------- */
518 int saa7164_api_get_fw_version(struct saa7164_dev
*dev
, u32
*version
);
519 int saa7164_api_enum_subdevs(struct saa7164_dev
*dev
);
520 int saa7164_api_i2c_read(struct saa7164_i2c
*bus
, u8 addr
, u32 reglen
, u8
*reg
,
521 u32 datalen
, u8
*data
);
522 int saa7164_api_i2c_write(struct saa7164_i2c
*bus
, u8 addr
,
523 u32 datalen
, u8
*data
);
524 int saa7164_api_dif_write(struct saa7164_i2c
*bus
, u8 addr
,
525 u32 datalen
, u8
*data
);
526 int saa7164_api_read_eeprom(struct saa7164_dev
*dev
, u8
*buf
, int buflen
);
527 int saa7164_api_set_gpiobit(struct saa7164_dev
*dev
, u8 unitid
, u8 pin
);
528 int saa7164_api_clear_gpiobit(struct saa7164_dev
*dev
, u8 unitid
, u8 pin
);
529 int saa7164_api_transition_port(struct saa7164_port
*port
, u8 mode
);
530 int saa7164_api_initialize_dif(struct saa7164_port
*port
);
531 int saa7164_api_configure_dif(struct saa7164_port
*port
, u32 std
);
532 int saa7164_api_set_encoder(struct saa7164_port
*port
);
533 int saa7164_api_get_encoder(struct saa7164_port
*port
);
534 int saa7164_api_set_aspect_ratio(struct saa7164_port
*port
);
535 int saa7164_api_set_usercontrol(struct saa7164_port
*port
, u8 ctl
);
536 int saa7164_api_get_usercontrol(struct saa7164_port
*port
, u8 ctl
);
537 int saa7164_api_set_videomux(struct saa7164_port
*port
);
538 int saa7164_api_audio_mute(struct saa7164_port
*port
, int mute
);
539 int saa7164_api_set_audio_volume(struct saa7164_port
*port
, s8 level
);
540 int saa7164_api_set_audio_std(struct saa7164_port
*port
);
541 int saa7164_api_set_audio_detection(struct saa7164_port
*port
, int autodetect
);
542 int saa7164_api_get_videomux(struct saa7164_port
*port
);
543 int saa7164_api_set_vbi_format(struct saa7164_port
*port
);
544 int saa7164_api_set_debug(struct saa7164_dev
*dev
, u8 level
);
545 int saa7164_api_collect_debug(struct saa7164_dev
*dev
);
546 int saa7164_api_get_load_info(struct saa7164_dev
*dev
,
547 struct tmFwInfoStruct
*i
);
549 /* ----------------------------------------------------------- */
550 /* saa7164-cards.c */
551 extern struct saa7164_board saa7164_boards
[];
552 extern const unsigned int saa7164_bcount
;
554 extern struct saa7164_subid saa7164_subids
[];
555 extern const unsigned int saa7164_idcount
;
557 extern void saa7164_card_list(struct saa7164_dev
*dev
);
558 extern void saa7164_gpio_setup(struct saa7164_dev
*dev
);
559 extern void saa7164_card_setup(struct saa7164_dev
*dev
);
561 extern int saa7164_i2caddr_to_reglen(struct saa7164_i2c
*bus
, int addr
);
562 extern int saa7164_i2caddr_to_unitid(struct saa7164_i2c
*bus
, int addr
);
563 extern char *saa7164_unitid_name(struct saa7164_dev
*dev
, u8 unitid
);
565 /* ----------------------------------------------------------- */
567 extern int saa7164_dvb_register(struct saa7164_port
*port
);
568 extern int saa7164_dvb_unregister(struct saa7164_port
*port
);
570 /* ----------------------------------------------------------- */
571 /* saa7164-buffer.c */
572 extern struct saa7164_buffer
*saa7164_buffer_alloc(
573 struct saa7164_port
*port
, u32 len
);
574 extern int saa7164_buffer_dealloc(struct saa7164_buffer
*buf
);
575 extern void saa7164_buffer_display(struct saa7164_buffer
*buf
);
576 extern int saa7164_buffer_activate(struct saa7164_buffer
*buf
, int i
);
577 extern int saa7164_buffer_cfg_port(struct saa7164_port
*port
);
578 extern struct saa7164_user_buffer
*saa7164_buffer_alloc_user(
579 struct saa7164_dev
*dev
, u32 len
);
580 extern void saa7164_buffer_dealloc_user(struct saa7164_user_buffer
*buf
);
581 extern int saa7164_buffer_zero_offsets(struct saa7164_port
*port
, int i
);
583 /* ----------------------------------------------------------- */
584 /* saa7164-encoder.c */
585 int saa7164_s_std(struct saa7164_port
*port
, v4l2_std_id id
);
586 int saa7164_g_std(struct saa7164_port
*port
, v4l2_std_id
*id
);
587 int saa7164_enum_input(struct file
*file
, void *priv
, struct v4l2_input
*i
);
588 int saa7164_g_input(struct saa7164_port
*port
, unsigned int *i
);
589 int saa7164_s_input(struct saa7164_port
*port
, unsigned int i
);
590 int saa7164_g_tuner(struct file
*file
, void *priv
, struct v4l2_tuner
*t
);
591 int saa7164_s_tuner(struct file
*file
, void *priv
, const struct v4l2_tuner
*t
);
592 int saa7164_g_frequency(struct saa7164_port
*port
, struct v4l2_frequency
*f
);
593 int saa7164_s_frequency(struct saa7164_port
*port
,
594 const struct v4l2_frequency
*f
);
595 int saa7164_encoder_register(struct saa7164_port
*port
);
596 void saa7164_encoder_unregister(struct saa7164_port
*port
);
598 /* ----------------------------------------------------------- */
600 int saa7164_vbi_register(struct saa7164_port
*port
);
601 void saa7164_vbi_unregister(struct saa7164_port
*port
);
603 /* ----------------------------------------------------------- */
605 extern unsigned int crc_checking
;
607 extern unsigned int saa_debug
;
608 #define dprintk(level, fmt, arg...)\
609 do { if (saa_debug & level)\
610 printk(KERN_DEBUG "%s: " fmt, dev->name, ## arg);\
613 #define log_warn(fmt, arg...)\
615 printk(KERN_WARNING "%s: " fmt, dev->name, ## arg);\
618 #define saa7164_readl(reg) readl(dev->lmmio + ((reg) >> 2))
619 #define saa7164_writel(reg, value) writel((value), dev->lmmio + ((reg) >> 2))
621 #define saa7164_readb(reg) readl(dev->bmmio + (reg))
622 #define saa7164_writeb(reg, value) writel((value), dev->bmmio + (reg))