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.
22 saa7164_core.c/buffer.c/cards.c/i2c.c/dvb.c
23 | : Standard Linux driver framework for creating
24 | : exposing and managing interfaces to the rest
25 | : of the kernel or userland. Also uses _fw.c to load
26 | : firmware direct into the PCIe bus, bypassing layers.
28 saa7164_api..() : Translate kernel specific functions/features
29 | : into command buffers.
31 saa7164_cmd..() : Manages the flow of command packets on/off,
32 | : the bus. Deal with bus errors, timeouts etc.
34 saa7164_bus..() : Manage a read/write memory ring buffer in the
35 | : PCIe Address space.
37 | saa7164_fw...() : Load any frimware
38 | | : direct into the device
40 <- ----------------- PCIe address space -------------------- ->
43 #include <linux/pci.h>
44 #include <linux/i2c.h>
45 #include <linux/kdev_t.h>
46 #include <linux/mutex.h>
47 #include <linux/crc32.h>
48 #include <linux/kthread.h>
49 #include <linux/freezer.h>
51 #include <media/tuner.h>
52 #include <media/tveeprom.h>
53 #include <media/dvb_demux.h>
54 #include <media/dvb_frontend.h>
55 #include <media/dvb_net.h>
56 #include <media/dvbdev.h>
57 #include <media/dmxdev.h>
58 #include <media/v4l2-common.h>
59 #include <media/v4l2-ioctl.h>
60 #include <media/v4l2-device.h>
61 #include <media/v4l2-ctrls.h>
62 #include <media/v4l2-event.h>
64 #include "saa7164-reg.h"
65 #include "saa7164-types.h"
67 #define SAA7164_MAXBOARDS 8
70 #define SAA7164_BOARD_NOAUTO UNSET
71 #define SAA7164_BOARD_UNKNOWN 0
72 #define SAA7164_BOARD_UNKNOWN_REV2 1
73 #define SAA7164_BOARD_UNKNOWN_REV3 2
74 #define SAA7164_BOARD_HAUPPAUGE_HVR2250 3
75 #define SAA7164_BOARD_HAUPPAUGE_HVR2200 4
76 #define SAA7164_BOARD_HAUPPAUGE_HVR2200_2 5
77 #define SAA7164_BOARD_HAUPPAUGE_HVR2200_3 6
78 #define SAA7164_BOARD_HAUPPAUGE_HVR2250_2 7
79 #define SAA7164_BOARD_HAUPPAUGE_HVR2250_3 8
80 #define SAA7164_BOARD_HAUPPAUGE_HVR2200_4 9
81 #define SAA7164_BOARD_HAUPPAUGE_HVR2200_5 10
82 #define SAA7164_BOARD_HAUPPAUGE_HVR2255proto 11
83 #define SAA7164_BOARD_HAUPPAUGE_HVR2255 12
84 #define SAA7164_BOARD_HAUPPAUGE_HVR2205 13
86 #define SAA7164_MAX_UNITS 8
87 #define SAA7164_TS_NUMBER_OF_LINES 312
88 #define SAA7164_PS_NUMBER_OF_LINES 256
89 #define SAA7164_PT_ENTRIES 16 /* (312 * 188) / 4096 */
90 #define SAA7164_MAX_ENCODER_BUFFERS 64 /* max 5secs of latency at 6Mbps */
91 #define SAA7164_MAX_VBI_BUFFERS 64
93 /* Port related defines */
94 #define SAA7164_PORT_TS1 (0)
95 #define SAA7164_PORT_TS2 (SAA7164_PORT_TS1 + 1)
96 #define SAA7164_PORT_ENC1 (SAA7164_PORT_TS2 + 1)
97 #define SAA7164_PORT_ENC2 (SAA7164_PORT_ENC1 + 1)
98 #define SAA7164_PORT_VBI1 (SAA7164_PORT_ENC2 + 1)
99 #define SAA7164_PORT_VBI2 (SAA7164_PORT_VBI1 + 1)
100 #define SAA7164_MAX_PORTS (SAA7164_PORT_VBI2 + 1)
104 #define DBGLVL_I2C 16
105 #define DBGLVL_API 32
106 #define DBGLVL_CMD 64
107 #define DBGLVL_BUS 128
108 #define DBGLVL_IRQ 256
109 #define DBGLVL_BUF 512
110 #define DBGLVL_ENC 1024
111 #define DBGLVL_VBI 2048
112 #define DBGLVL_THR 4096
113 #define DBGLVL_CPU 8192
115 #define SAA7164_NORMS \
116 (V4L2_STD_NTSC_M | V4L2_STD_NTSC_M_JP)
118 /* TV frequency range copied from tuner-core.c */
119 #define SAA7164_TV_MIN_FREQ (44U * 16U)
120 #define SAA7164_TV_MAX_FREQ (958U * 16U)
123 SAA7164_MPEG_UNDEFINED
= 0,
125 SAA7164_MPEG_ENCODER
,
129 enum saa7164_i2c_bus_nr
{
130 SAA7164_I2C_BUS_0
= 0,
135 enum saa7164_buffer_flags
{
136 SAA7164_BUFFER_UNDEFINED
= 0,
142 enum saa7164_unit_type
{
143 SAA7164_UNIT_UNDEFINED
= 0,
144 SAA7164_UNIT_DIGITAL_DEMODULATOR
,
145 SAA7164_UNIT_ANALOG_DEMODULATOR
,
148 SAA7164_UNIT_ZILOG_IRBLASTER
,
149 SAA7164_UNIT_ENCODER
,
152 /* The PCIe bridge doesn't grant direct access to i2c.
153 * Instead, you address i2c devices using a uniqely
154 * allocated 'unitid' value via a messaging API. This
155 * is a problem. The kernel and existing demod/tuner
156 * drivers expect to talk 'i2c', so we have to maintain
157 * a translation layer, and a series of functions to
158 * convert i2c bus + device address into a unit id.
160 struct saa7164_unit
{
161 enum saa7164_unit_type type
;
164 enum saa7164_i2c_bus_nr i2c_bus_nr
;
169 struct saa7164_board
{
171 enum port_t porta
, portb
, portc
,
174 SAA7164_CHIP_UNDEFINED
= 0,
178 struct saa7164_unit unit
[SAA7164_MAX_UNITS
];
181 struct saa7164_subid
{
187 struct saa7164_encoder_fh
{
189 struct saa7164_port
*port
;
190 atomic_t v4l_reading
;
193 struct saa7164_vbi_fh
{
195 struct saa7164_port
*port
;
196 atomic_t v4l_reading
;
199 struct saa7164_histogram_bucket
{
205 struct saa7164_histogram
{
207 struct saa7164_histogram_bucket counter1
[64];
210 struct saa7164_user_buffer
{
211 struct list_head list
;
221 struct saa7164_fw_status
{
223 /* RISC Core details */
231 /* Firmware version */
241 struct dvb_adapter adapter
;
242 struct dvb_frontend
*frontend
;
243 struct dvb_demux demux
;
244 struct dmxdev dmxdev
;
245 struct dmx_frontend fe_hw
;
246 struct dmx_frontend fe_mem
;
252 struct saa7164_dev
*dev
;
254 enum saa7164_i2c_bus_nr nr
;
257 struct i2c_adapter i2c_adap
;
258 struct i2c_client i2c_client
;
262 struct saa7164_tvnorm
{
267 struct saa7164_encoder_params
{
268 struct saa7164_tvnorm encodernorm
;
272 u32 bitrate
; /* bps */
273 u32 bitrate_peak
; /* bps */
275 u32 stream_type
; /* V4L2_MPEG_STREAM_TYPE_MPEG2_TS */
277 u32 audio_sampling_freq
;
284 struct saa7164_vbi_params
{
285 struct saa7164_tvnorm encodernorm
;
289 u32 bitrate
; /* bps */
290 u32 bitrate_peak
; /* bps */
292 u32 stream_type
; /* V4L2_MPEG_STREAM_TYPE_MPEG2_TS */
294 u32 audio_sampling_freq
;
303 struct saa7164_buffer
{
304 struct list_head list
;
306 /* Note of which h/w buffer list index position we occupy */
309 struct saa7164_port
*port
;
311 /* Hardware Specific */
312 /* PCI Memory allocations */
313 enum saa7164_buffer_flags flags
; /* Free, Busy, Full */
315 /* A block of page align PCI memory */
316 u32 pci_size
; /* PCI allocation size in bytes */
317 u64
*cpu
; /* Virtual address */
318 dma_addr_t dma
; /* Physical address */
319 u32 crc
; /* Checksum for the entire buffer data */
321 /* A page table that splits the block into a number of entries */
322 u32 pt_size
; /* PCI allocation size in bytes */
323 u64
*pt_cpu
; /* Virtual address */
324 dma_addr_t pt_dma
; /* Physical address */
331 struct saa7164_port
{
333 struct saa7164_dev
*dev
;
337 /* --- Generic port attributes --- */
339 /* HW stream parameters */
340 struct tmHWStreamParameters hw_streamingparams
;
342 /* DMA configuration values, is seeded during initialization */
343 struct tmComResDMATermDescrHeader hwcfg
;
345 /* hardware specific registers */
354 u32 numpte
; /* Number of entries in array, only valid in head */
356 struct mutex dmaqueue_lock
;
357 struct saa7164_buffer dmaqueue
;
359 u64 last_irq_msecs
, last_svc_msecs
;
360 u64 last_irq_msecs_diff
, last_svc_msecs_diff
;
363 u64 last_irq_svc_msecs_diff
;
364 u64 last_read_msecs
, last_read_msecs_diff
;
365 u64 last_poll_msecs
, last_poll_msecs_diff
;
367 struct saa7164_histogram irq_interval
;
368 struct saa7164_histogram svc_interval
;
369 struct saa7164_histogram irq_svc_interval
;
370 struct saa7164_histogram read_interval
;
371 struct saa7164_histogram poll_interval
;
373 /* --- DVB Transport Specific --- */
374 struct saa7164_dvb dvb
;
375 struct i2c_client
*i2c_client_demod
;
376 struct i2c_client
*i2c_client_tuner
;
378 /* --- Encoder/V4L related attributes --- */
380 /* Defaults established in saa7164-encoder.c */
381 struct saa7164_tvnorm encodernorm
;
382 struct v4l2_ctrl_handler ctrl_handler
;
399 struct tmComResAFeatureDescrHeader audfeat
;
400 struct tmComResEncoderDescrHeader encunit
;
401 struct tmComResProcDescrHeader vidproc
;
402 struct tmComResExtDevDescrHeader ifunit
;
403 struct tmComResTunerDescrHeader tunerunit
;
405 struct work_struct workenc
;
407 /* V4L Encoder Video */
408 struct saa7164_encoder_params encoder_params
;
409 struct video_device
*v4l_device
;
410 atomic_t v4l_reader_count
;
412 struct saa7164_buffer list_buf_used
;
413 struct saa7164_buffer list_buf_free
;
414 wait_queue_head_t wait_read
;
417 struct tmComResVBIFormatDescrHeader vbi_fmt_ntsc
;
418 struct saa7164_vbi_params vbi_params
;
419 struct saa7164_port
*enc_port
;
427 u32 done_first_interrupt
;
431 struct list_head devlist
;
434 struct v4l2_device v4l2_dev
;
438 unsigned char pci_rev
, pci_lat
;
439 int pci_bus
, pci_slot
;
452 /* firmware status */
453 struct saa7164_fw_status fw_status
;
456 struct tmComResHWDescr hwdesc
;
457 struct tmComResInterfaceDescr intfdesc
;
458 struct tmComResBusDescr busdesc
;
460 struct tmComResBusInfo bus
;
462 /* Interrupt status and ack registers */
467 struct cmd cmds
[SAA_CMD_MAX_MSG_UNITS
];
471 struct saa7164_i2c i2c_bus
[3];
473 /* Transport related */
474 struct saa7164_port ports
[SAA7164_MAX_PORTS
];
476 /* Deferred command/api interrupts handling */
477 struct work_struct workcmd
;
479 /* A kernel thread to monitor the firmware log, used
480 * only in debug mode.
482 struct task_struct
*kthread
;
486 extern struct list_head saa7164_devlist
;
487 extern unsigned int waitsecs
;
488 extern unsigned int encoder_buffers
;
489 extern unsigned int vbi_buffers
;
491 /* ----------------------------------------------------------- */
493 void saa7164_dumpregs(struct saa7164_dev
*dev
, u32 addr
);
494 void saa7164_getfirmwarestatus(struct saa7164_dev
*dev
);
495 u32
saa7164_getcurrentfirmwareversion(struct saa7164_dev
*dev
);
496 void saa7164_histogram_update(struct saa7164_histogram
*hg
, u32 val
);
498 /* ----------------------------------------------------------- */
500 int saa7164_downloadfirmware(struct saa7164_dev
*dev
);
502 /* ----------------------------------------------------------- */
504 extern int saa7164_i2c_register(struct saa7164_i2c
*bus
);
505 extern int saa7164_i2c_unregister(struct saa7164_i2c
*bus
);
506 extern void saa7164_call_i2c_clients(struct saa7164_i2c
*bus
,
507 unsigned int cmd
, void *arg
);
509 /* ----------------------------------------------------------- */
511 int saa7164_bus_setup(struct saa7164_dev
*dev
);
512 void saa7164_bus_dump(struct saa7164_dev
*dev
);
513 int saa7164_bus_set(struct saa7164_dev
*dev
, struct tmComResInfo
* msg
,
515 int saa7164_bus_get(struct saa7164_dev
*dev
, struct tmComResInfo
* msg
,
516 void *buf
, int peekonly
);
518 /* ----------------------------------------------------------- */
520 int saa7164_cmd_send(struct saa7164_dev
*dev
,
521 u8 id
, enum tmComResCmd command
, u16 controlselector
,
522 u16 size
, void *buf
);
523 void saa7164_cmd_signal(struct saa7164_dev
*dev
, u8 seqno
);
524 int saa7164_irq_dequeue(struct saa7164_dev
*dev
);
526 /* ----------------------------------------------------------- */
528 int saa7164_api_get_fw_version(struct saa7164_dev
*dev
, u32
*version
);
529 int saa7164_api_enum_subdevs(struct saa7164_dev
*dev
);
530 int saa7164_api_i2c_read(struct saa7164_i2c
*bus
, u8 addr
, u32 reglen
, u8
*reg
,
531 u32 datalen
, u8
*data
);
532 int saa7164_api_i2c_write(struct saa7164_i2c
*bus
, u8 addr
,
533 u32 datalen
, u8
*data
);
534 int saa7164_api_dif_write(struct saa7164_i2c
*bus
, u8 addr
,
535 u32 datalen
, u8
*data
);
536 int saa7164_api_read_eeprom(struct saa7164_dev
*dev
, u8
*buf
, int buflen
);
537 int saa7164_api_set_gpiobit(struct saa7164_dev
*dev
, u8 unitid
, u8 pin
);
538 int saa7164_api_clear_gpiobit(struct saa7164_dev
*dev
, u8 unitid
, u8 pin
);
539 int saa7164_api_transition_port(struct saa7164_port
*port
, u8 mode
);
540 int saa7164_api_initialize_dif(struct saa7164_port
*port
);
541 int saa7164_api_configure_dif(struct saa7164_port
*port
, u32 std
);
542 int saa7164_api_set_encoder(struct saa7164_port
*port
);
543 int saa7164_api_get_encoder(struct saa7164_port
*port
);
544 int saa7164_api_set_aspect_ratio(struct saa7164_port
*port
);
545 int saa7164_api_set_usercontrol(struct saa7164_port
*port
, u8 ctl
);
546 int saa7164_api_get_usercontrol(struct saa7164_port
*port
, u8 ctl
);
547 int saa7164_api_set_videomux(struct saa7164_port
*port
);
548 int saa7164_api_audio_mute(struct saa7164_port
*port
, int mute
);
549 int saa7164_api_set_audio_volume(struct saa7164_port
*port
, s8 level
);
550 int saa7164_api_set_audio_std(struct saa7164_port
*port
);
551 int saa7164_api_set_audio_detection(struct saa7164_port
*port
, int autodetect
);
552 int saa7164_api_get_videomux(struct saa7164_port
*port
);
553 int saa7164_api_set_vbi_format(struct saa7164_port
*port
);
554 int saa7164_api_set_debug(struct saa7164_dev
*dev
, u8 level
);
555 int saa7164_api_collect_debug(struct saa7164_dev
*dev
);
556 int saa7164_api_get_load_info(struct saa7164_dev
*dev
,
557 struct tmFwInfoStruct
*i
);
559 /* ----------------------------------------------------------- */
560 /* saa7164-cards.c */
561 extern struct saa7164_board saa7164_boards
[];
562 extern const unsigned int saa7164_bcount
;
564 extern struct saa7164_subid saa7164_subids
[];
565 extern const unsigned int saa7164_idcount
;
567 extern void saa7164_card_list(struct saa7164_dev
*dev
);
568 extern void saa7164_gpio_setup(struct saa7164_dev
*dev
);
569 extern void saa7164_card_setup(struct saa7164_dev
*dev
);
571 extern int saa7164_i2caddr_to_reglen(struct saa7164_i2c
*bus
, int addr
);
572 extern int saa7164_i2caddr_to_unitid(struct saa7164_i2c
*bus
, int addr
);
573 extern char *saa7164_unitid_name(struct saa7164_dev
*dev
, u8 unitid
);
575 /* ----------------------------------------------------------- */
577 extern int saa7164_dvb_register(struct saa7164_port
*port
);
578 extern int saa7164_dvb_unregister(struct saa7164_port
*port
);
580 /* ----------------------------------------------------------- */
581 /* saa7164-buffer.c */
582 extern struct saa7164_buffer
*saa7164_buffer_alloc(
583 struct saa7164_port
*port
, u32 len
);
584 extern int saa7164_buffer_dealloc(struct saa7164_buffer
*buf
);
585 extern void saa7164_buffer_display(struct saa7164_buffer
*buf
);
586 extern int saa7164_buffer_activate(struct saa7164_buffer
*buf
, int i
);
587 extern int saa7164_buffer_cfg_port(struct saa7164_port
*port
);
588 extern struct saa7164_user_buffer
*saa7164_buffer_alloc_user(
589 struct saa7164_dev
*dev
, u32 len
);
590 extern void saa7164_buffer_dealloc_user(struct saa7164_user_buffer
*buf
);
591 extern int saa7164_buffer_zero_offsets(struct saa7164_port
*port
, int i
);
593 /* ----------------------------------------------------------- */
594 /* saa7164-encoder.c */
595 int saa7164_s_std(struct saa7164_port
*port
, v4l2_std_id id
);
596 int saa7164_g_std(struct saa7164_port
*port
, v4l2_std_id
*id
);
597 int saa7164_enum_input(struct file
*file
, void *priv
, struct v4l2_input
*i
);
598 int saa7164_g_input(struct saa7164_port
*port
, unsigned int *i
);
599 int saa7164_s_input(struct saa7164_port
*port
, unsigned int i
);
600 int saa7164_g_tuner(struct file
*file
, void *priv
, struct v4l2_tuner
*t
);
601 int saa7164_s_tuner(struct file
*file
, void *priv
, const struct v4l2_tuner
*t
);
602 int saa7164_g_frequency(struct saa7164_port
*port
, struct v4l2_frequency
*f
);
603 int saa7164_s_frequency(struct saa7164_port
*port
,
604 const struct v4l2_frequency
*f
);
605 int saa7164_encoder_register(struct saa7164_port
*port
);
606 void saa7164_encoder_unregister(struct saa7164_port
*port
);
608 /* ----------------------------------------------------------- */
610 int saa7164_vbi_register(struct saa7164_port
*port
);
611 void saa7164_vbi_unregister(struct saa7164_port
*port
);
613 /* ----------------------------------------------------------- */
615 extern unsigned int crc_checking
;
617 extern unsigned int saa_debug
;
618 #define dprintk(level, fmt, arg...)\
619 do { if (saa_debug & level)\
620 printk(KERN_DEBUG "%s: " fmt, dev->name, ## arg);\
623 #define log_warn(fmt, arg...)\
625 printk(KERN_WARNING "%s: " fmt, dev->name, ## arg);\
628 #define saa7164_readl(reg) readl(dev->lmmio + ((reg) >> 2))
629 #define saa7164_writel(reg, value) writel((value), dev->lmmio + ((reg) >> 2))
631 #define saa7164_readb(reg) readl(dev->bmmio + (reg))
632 #define saa7164_writeb(reg, value) writel((value), dev->bmmio + (reg))