2 * Driver for the NXP SAA7164 PCIe bridge
4 * Copyright (c) 2010-2015 Steven Toth <stoth@kernellabs.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 saa7164_core.c/buffer.c/cards.c/i2c.c/dvb.c
27 | : Standard Linux driver framework for creating
28 | : exposing and managing interfaces to the rest
29 | : of the kernel or userland. Also uses _fw.c to load
30 | : firmware direct into the PCIe bus, bypassing layers.
32 saa7164_api..() : Translate kernel specific functions/features
33 | : into command buffers.
35 saa7164_cmd..() : Manages the flow of command packets on/off,
36 | : the bus. Deal with bus errors, timeouts etc.
38 saa7164_bus..() : Manage a read/write memory ring buffer in the
39 | : PCIe Address space.
41 | saa7164_fw...() : Load any frimware
42 | | : direct into the device
44 <- ----------------- PCIe address space -------------------- ->
47 #include <linux/pci.h>
48 #include <linux/i2c.h>
49 #include <linux/kdev_t.h>
50 #include <linux/mutex.h>
51 #include <linux/crc32.h>
52 #include <linux/kthread.h>
53 #include <linux/freezer.h>
55 #include <media/tuner.h>
56 #include <media/tveeprom.h>
57 #include <dvb_demux.h>
58 #include <dvb_frontend.h>
62 #include <media/v4l2-common.h>
63 #include <media/v4l2-ioctl.h>
64 #include <media/v4l2-device.h>
65 #include <media/v4l2-ctrls.h>
66 #include <media/v4l2-event.h>
68 #include "saa7164-reg.h"
69 #include "saa7164-types.h"
71 #define SAA7164_MAXBOARDS 8
74 #define SAA7164_BOARD_NOAUTO UNSET
75 #define SAA7164_BOARD_UNKNOWN 0
76 #define SAA7164_BOARD_UNKNOWN_REV2 1
77 #define SAA7164_BOARD_UNKNOWN_REV3 2
78 #define SAA7164_BOARD_HAUPPAUGE_HVR2250 3
79 #define SAA7164_BOARD_HAUPPAUGE_HVR2200 4
80 #define SAA7164_BOARD_HAUPPAUGE_HVR2200_2 5
81 #define SAA7164_BOARD_HAUPPAUGE_HVR2200_3 6
82 #define SAA7164_BOARD_HAUPPAUGE_HVR2250_2 7
83 #define SAA7164_BOARD_HAUPPAUGE_HVR2250_3 8
84 #define SAA7164_BOARD_HAUPPAUGE_HVR2200_4 9
85 #define SAA7164_BOARD_HAUPPAUGE_HVR2200_5 10
86 #define SAA7164_BOARD_HAUPPAUGE_HVR2255proto 11
87 #define SAA7164_BOARD_HAUPPAUGE_HVR2255 12
88 #define SAA7164_BOARD_HAUPPAUGE_HVR2205 13
90 #define SAA7164_MAX_UNITS 8
91 #define SAA7164_TS_NUMBER_OF_LINES 312
92 #define SAA7164_PS_NUMBER_OF_LINES 256
93 #define SAA7164_PT_ENTRIES 16 /* (312 * 188) / 4096 */
94 #define SAA7164_MAX_ENCODER_BUFFERS 64 /* max 5secs of latency at 6Mbps */
95 #define SAA7164_MAX_VBI_BUFFERS 64
97 /* Port related defines */
98 #define SAA7164_PORT_TS1 (0)
99 #define SAA7164_PORT_TS2 (SAA7164_PORT_TS1 + 1)
100 #define SAA7164_PORT_ENC1 (SAA7164_PORT_TS2 + 1)
101 #define SAA7164_PORT_ENC2 (SAA7164_PORT_ENC1 + 1)
102 #define SAA7164_PORT_VBI1 (SAA7164_PORT_ENC2 + 1)
103 #define SAA7164_PORT_VBI2 (SAA7164_PORT_VBI1 + 1)
104 #define SAA7164_MAX_PORTS (SAA7164_PORT_VBI2 + 1)
108 #define DBGLVL_I2C 16
109 #define DBGLVL_API 32
110 #define DBGLVL_CMD 64
111 #define DBGLVL_BUS 128
112 #define DBGLVL_IRQ 256
113 #define DBGLVL_BUF 512
114 #define DBGLVL_ENC 1024
115 #define DBGLVL_VBI 2048
116 #define DBGLVL_THR 4096
117 #define DBGLVL_CPU 8192
119 #define SAA7164_NORMS \
120 (V4L2_STD_NTSC_M | V4L2_STD_NTSC_M_JP)
122 /* TV frequency range copied from tuner-core.c */
123 #define SAA7164_TV_MIN_FREQ (44U * 16U)
124 #define SAA7164_TV_MAX_FREQ (958U * 16U)
127 SAA7164_MPEG_UNDEFINED
= 0,
129 SAA7164_MPEG_ENCODER
,
133 enum saa7164_i2c_bus_nr
{
134 SAA7164_I2C_BUS_0
= 0,
139 enum saa7164_buffer_flags
{
140 SAA7164_BUFFER_UNDEFINED
= 0,
146 enum saa7164_unit_type
{
147 SAA7164_UNIT_UNDEFINED
= 0,
148 SAA7164_UNIT_DIGITAL_DEMODULATOR
,
149 SAA7164_UNIT_ANALOG_DEMODULATOR
,
152 SAA7164_UNIT_ZILOG_IRBLASTER
,
153 SAA7164_UNIT_ENCODER
,
156 /* The PCIe bridge doesn't grant direct access to i2c.
157 * Instead, you address i2c devices using a uniqely
158 * allocated 'unitid' value via a messaging API. This
159 * is a problem. The kernel and existing demod/tuner
160 * drivers expect to talk 'i2c', so we have to maintain
161 * a translation layer, and a series of functions to
162 * convert i2c bus + device address into a unit id.
164 struct saa7164_unit
{
165 enum saa7164_unit_type type
;
168 enum saa7164_i2c_bus_nr i2c_bus_nr
;
173 struct saa7164_board
{
175 enum port_t porta
, portb
, portc
,
178 SAA7164_CHIP_UNDEFINED
= 0,
182 struct saa7164_unit unit
[SAA7164_MAX_UNITS
];
185 struct saa7164_subid
{
191 struct saa7164_encoder_fh
{
193 struct saa7164_port
*port
;
194 atomic_t v4l_reading
;
197 struct saa7164_vbi_fh
{
199 struct saa7164_port
*port
;
200 atomic_t v4l_reading
;
203 struct saa7164_histogram_bucket
{
209 struct saa7164_histogram
{
211 struct saa7164_histogram_bucket counter1
[64];
214 struct saa7164_user_buffer
{
215 struct list_head list
;
225 struct saa7164_fw_status
{
227 /* RISC Core details */
235 /* Firmware version */
245 struct dvb_adapter adapter
;
246 struct dvb_frontend
*frontend
;
247 struct dvb_demux demux
;
248 struct dmxdev dmxdev
;
249 struct dmx_frontend fe_hw
;
250 struct dmx_frontend fe_mem
;
256 struct saa7164_dev
*dev
;
258 enum saa7164_i2c_bus_nr nr
;
261 struct i2c_adapter i2c_adap
;
262 struct i2c_client i2c_client
;
266 struct saa7164_ctrl
{
267 struct v4l2_queryctrl v
;
270 struct saa7164_tvnorm
{
275 struct saa7164_encoder_params
{
276 struct saa7164_tvnorm encodernorm
;
280 u32 bitrate
; /* bps */
281 u32 bitrate_peak
; /* bps */
283 u32 stream_type
; /* V4L2_MPEG_STREAM_TYPE_MPEG2_TS */
285 u32 audio_sampling_freq
;
292 struct saa7164_vbi_params
{
293 struct saa7164_tvnorm encodernorm
;
297 u32 bitrate
; /* bps */
298 u32 bitrate_peak
; /* bps */
300 u32 stream_type
; /* V4L2_MPEG_STREAM_TYPE_MPEG2_TS */
302 u32 audio_sampling_freq
;
311 struct saa7164_buffer
{
312 struct list_head list
;
314 /* Note of which h/w buffer list index position we occupy */
317 struct saa7164_port
*port
;
319 /* Hardware Specific */
320 /* PCI Memory allocations */
321 enum saa7164_buffer_flags flags
; /* Free, Busy, Full */
323 /* A block of page align PCI memory */
324 u32 pci_size
; /* PCI allocation size in bytes */
325 u64
*cpu
; /* Virtual address */
326 dma_addr_t dma
; /* Physical address */
327 u32 crc
; /* Checksum for the entire buffer data */
329 /* A page table that splits the block into a number of entries */
330 u32 pt_size
; /* PCI allocation size in bytes */
331 u64
*pt_cpu
; /* Virtual address */
332 dma_addr_t pt_dma
; /* Physical address */
339 struct saa7164_port
{
341 struct saa7164_dev
*dev
;
345 /* --- Generic port attributes --- */
347 /* HW stream parameters */
348 struct tmHWStreamParameters hw_streamingparams
;
350 /* DMA configuration values, is seeded during initialization */
351 struct tmComResDMATermDescrHeader hwcfg
;
353 /* hardware specific registers */
362 u32 numpte
; /* Number of entries in array, only valid in head */
364 struct mutex dmaqueue_lock
;
365 struct saa7164_buffer dmaqueue
;
367 u64 last_irq_msecs
, last_svc_msecs
;
368 u64 last_irq_msecs_diff
, last_svc_msecs_diff
;
371 u64 last_irq_svc_msecs_diff
;
372 u64 last_read_msecs
, last_read_msecs_diff
;
373 u64 last_poll_msecs
, last_poll_msecs_diff
;
375 struct saa7164_histogram irq_interval
;
376 struct saa7164_histogram svc_interval
;
377 struct saa7164_histogram irq_svc_interval
;
378 struct saa7164_histogram read_interval
;
379 struct saa7164_histogram poll_interval
;
381 /* --- DVB Transport Specific --- */
382 struct saa7164_dvb dvb
;
383 struct i2c_client
*i2c_client_demod
;
384 struct i2c_client
*i2c_client_tuner
;
386 /* --- Encoder/V4L related attributes --- */
388 /* Defaults established in saa7164-encoder.c */
389 struct saa7164_tvnorm encodernorm
;
390 struct v4l2_ctrl_handler ctrl_handler
;
407 struct tmComResAFeatureDescrHeader audfeat
;
408 struct tmComResEncoderDescrHeader encunit
;
409 struct tmComResProcDescrHeader vidproc
;
410 struct tmComResExtDevDescrHeader ifunit
;
411 struct tmComResTunerDescrHeader tunerunit
;
413 struct work_struct workenc
;
415 /* V4L Encoder Video */
416 struct saa7164_encoder_params encoder_params
;
417 struct video_device
*v4l_device
;
418 atomic_t v4l_reader_count
;
420 struct saa7164_buffer list_buf_used
;
421 struct saa7164_buffer list_buf_free
;
422 wait_queue_head_t wait_read
;
425 struct tmComResVBIFormatDescrHeader vbi_fmt_ntsc
;
426 struct saa7164_vbi_params vbi_params
;
427 struct saa7164_port
*enc_port
;
435 u32 done_first_interrupt
;
439 struct list_head devlist
;
442 struct v4l2_device v4l2_dev
;
446 unsigned char pci_rev
, pci_lat
;
447 int pci_bus
, pci_slot
;
460 /* firmware status */
461 struct saa7164_fw_status fw_status
;
464 struct tmComResHWDescr hwdesc
;
465 struct tmComResInterfaceDescr intfdesc
;
466 struct tmComResBusDescr busdesc
;
468 struct tmComResBusInfo bus
;
470 /* Interrupt status and ack registers */
475 struct cmd cmds
[SAA_CMD_MAX_MSG_UNITS
];
479 struct saa7164_i2c i2c_bus
[3];
481 /* Transport related */
482 struct saa7164_port ports
[SAA7164_MAX_PORTS
];
484 /* Deferred command/api interrupts handling */
485 struct work_struct workcmd
;
487 /* A kernel thread to monitor the firmware log, used
488 * only in debug mode.
490 struct task_struct
*kthread
;
494 extern struct list_head saa7164_devlist
;
495 extern unsigned int waitsecs
;
496 extern unsigned int encoder_buffers
;
497 extern unsigned int vbi_buffers
;
499 /* ----------------------------------------------------------- */
501 void saa7164_dumpregs(struct saa7164_dev
*dev
, u32 addr
);
502 void saa7164_getfirmwarestatus(struct saa7164_dev
*dev
);
503 u32
saa7164_getcurrentfirmwareversion(struct saa7164_dev
*dev
);
504 void saa7164_histogram_update(struct saa7164_histogram
*hg
, u32 val
);
506 /* ----------------------------------------------------------- */
508 int saa7164_downloadfirmware(struct saa7164_dev
*dev
);
510 /* ----------------------------------------------------------- */
512 extern int saa7164_i2c_register(struct saa7164_i2c
*bus
);
513 extern int saa7164_i2c_unregister(struct saa7164_i2c
*bus
);
514 extern void saa7164_call_i2c_clients(struct saa7164_i2c
*bus
,
515 unsigned int cmd
, void *arg
);
517 /* ----------------------------------------------------------- */
519 int saa7164_bus_setup(struct saa7164_dev
*dev
);
520 void saa7164_bus_dump(struct saa7164_dev
*dev
);
521 int saa7164_bus_set(struct saa7164_dev
*dev
, struct tmComResInfo
* msg
,
523 int saa7164_bus_get(struct saa7164_dev
*dev
, struct tmComResInfo
* msg
,
524 void *buf
, int peekonly
);
526 /* ----------------------------------------------------------- */
528 int saa7164_cmd_send(struct saa7164_dev
*dev
,
529 u8 id
, enum tmComResCmd command
, u16 controlselector
,
530 u16 size
, void *buf
);
531 void saa7164_cmd_signal(struct saa7164_dev
*dev
, u8 seqno
);
532 int saa7164_irq_dequeue(struct saa7164_dev
*dev
);
534 /* ----------------------------------------------------------- */
536 int saa7164_api_get_fw_version(struct saa7164_dev
*dev
, u32
*version
);
537 int saa7164_api_enum_subdevs(struct saa7164_dev
*dev
);
538 int saa7164_api_i2c_read(struct saa7164_i2c
*bus
, u8 addr
, u32 reglen
, u8
*reg
,
539 u32 datalen
, u8
*data
);
540 int saa7164_api_i2c_write(struct saa7164_i2c
*bus
, u8 addr
,
541 u32 datalen
, u8
*data
);
542 int saa7164_api_dif_write(struct saa7164_i2c
*bus
, u8 addr
,
543 u32 datalen
, u8
*data
);
544 int saa7164_api_read_eeprom(struct saa7164_dev
*dev
, u8
*buf
, int buflen
);
545 int saa7164_api_set_gpiobit(struct saa7164_dev
*dev
, u8 unitid
, u8 pin
);
546 int saa7164_api_clear_gpiobit(struct saa7164_dev
*dev
, u8 unitid
, u8 pin
);
547 int saa7164_api_transition_port(struct saa7164_port
*port
, u8 mode
);
548 int saa7164_api_initialize_dif(struct saa7164_port
*port
);
549 int saa7164_api_configure_dif(struct saa7164_port
*port
, u32 std
);
550 int saa7164_api_set_encoder(struct saa7164_port
*port
);
551 int saa7164_api_get_encoder(struct saa7164_port
*port
);
552 int saa7164_api_set_aspect_ratio(struct saa7164_port
*port
);
553 int saa7164_api_set_usercontrol(struct saa7164_port
*port
, u8 ctl
);
554 int saa7164_api_get_usercontrol(struct saa7164_port
*port
, u8 ctl
);
555 int saa7164_api_set_videomux(struct saa7164_port
*port
);
556 int saa7164_api_audio_mute(struct saa7164_port
*port
, int mute
);
557 int saa7164_api_set_audio_volume(struct saa7164_port
*port
, s8 level
);
558 int saa7164_api_set_audio_std(struct saa7164_port
*port
);
559 int saa7164_api_set_audio_detection(struct saa7164_port
*port
, int autodetect
);
560 int saa7164_api_get_videomux(struct saa7164_port
*port
);
561 int saa7164_api_set_vbi_format(struct saa7164_port
*port
);
562 int saa7164_api_set_debug(struct saa7164_dev
*dev
, u8 level
);
563 int saa7164_api_collect_debug(struct saa7164_dev
*dev
);
564 int saa7164_api_get_load_info(struct saa7164_dev
*dev
,
565 struct tmFwInfoStruct
*i
);
567 /* ----------------------------------------------------------- */
568 /* saa7164-cards.c */
569 extern struct saa7164_board saa7164_boards
[];
570 extern const unsigned int saa7164_bcount
;
572 extern struct saa7164_subid saa7164_subids
[];
573 extern const unsigned int saa7164_idcount
;
575 extern void saa7164_card_list(struct saa7164_dev
*dev
);
576 extern void saa7164_gpio_setup(struct saa7164_dev
*dev
);
577 extern void saa7164_card_setup(struct saa7164_dev
*dev
);
579 extern int saa7164_i2caddr_to_reglen(struct saa7164_i2c
*bus
, int addr
);
580 extern int saa7164_i2caddr_to_unitid(struct saa7164_i2c
*bus
, int addr
);
581 extern char *saa7164_unitid_name(struct saa7164_dev
*dev
, u8 unitid
);
583 /* ----------------------------------------------------------- */
585 extern int saa7164_dvb_register(struct saa7164_port
*port
);
586 extern int saa7164_dvb_unregister(struct saa7164_port
*port
);
588 /* ----------------------------------------------------------- */
589 /* saa7164-buffer.c */
590 extern struct saa7164_buffer
*saa7164_buffer_alloc(
591 struct saa7164_port
*port
, u32 len
);
592 extern int saa7164_buffer_dealloc(struct saa7164_buffer
*buf
);
593 extern void saa7164_buffer_display(struct saa7164_buffer
*buf
);
594 extern int saa7164_buffer_activate(struct saa7164_buffer
*buf
, int i
);
595 extern int saa7164_buffer_cfg_port(struct saa7164_port
*port
);
596 extern struct saa7164_user_buffer
*saa7164_buffer_alloc_user(
597 struct saa7164_dev
*dev
, u32 len
);
598 extern void saa7164_buffer_dealloc_user(struct saa7164_user_buffer
*buf
);
599 extern int saa7164_buffer_zero_offsets(struct saa7164_port
*port
, int i
);
601 /* ----------------------------------------------------------- */
602 /* saa7164-encoder.c */
603 int saa7164_s_std(struct saa7164_port
*port
, v4l2_std_id id
);
604 int saa7164_g_std(struct saa7164_port
*port
, v4l2_std_id
*id
);
605 int saa7164_enum_input(struct file
*file
, void *priv
, struct v4l2_input
*i
);
606 int saa7164_g_input(struct saa7164_port
*port
, unsigned int *i
);
607 int saa7164_s_input(struct saa7164_port
*port
, unsigned int i
);
608 int saa7164_g_tuner(struct file
*file
, void *priv
, struct v4l2_tuner
*t
);
609 int saa7164_s_tuner(struct file
*file
, void *priv
, const struct v4l2_tuner
*t
);
610 int saa7164_g_frequency(struct saa7164_port
*port
, struct v4l2_frequency
*f
);
611 int saa7164_s_frequency(struct saa7164_port
*port
,
612 const struct v4l2_frequency
*f
);
613 int saa7164_encoder_register(struct saa7164_port
*port
);
614 void saa7164_encoder_unregister(struct saa7164_port
*port
);
616 /* ----------------------------------------------------------- */
618 int saa7164_vbi_register(struct saa7164_port
*port
);
619 void saa7164_vbi_unregister(struct saa7164_port
*port
);
621 /* ----------------------------------------------------------- */
623 extern unsigned int crc_checking
;
625 extern unsigned int saa_debug
;
626 #define dprintk(level, fmt, arg...)\
627 do { if (saa_debug & level)\
628 printk(KERN_DEBUG "%s: " fmt, dev->name, ## arg);\
631 #define log_warn(fmt, arg...)\
633 printk(KERN_WARNING "%s: " fmt, dev->name, ## arg);\
636 #define saa7164_readl(reg) readl(dev->lmmio + ((reg) >> 2))
637 #define saa7164_writel(reg, value) writel((value), dev->lmmio + ((reg) >> 2))
639 #define saa7164_readb(reg) readl(dev->bmmio + (reg))
640 #define saa7164_writeb(reg, value) writel((value), dev->bmmio + (reg))