Merge tag 'block-5.11-2021-01-10' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / media / pci / saa7164 / saa7164.h
blob2801a2b03fa0963cb5e40896adb129d8fde69908
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3 * Driver for the NXP SAA7164 PCIe bridge
5 * Copyright (c) 2010-2015 Steven Toth <stoth@kernellabs.com>
6 */
8 /*
9 Driver architecture
10 *******************
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
29 V V
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
59 #define UNSET (-1U)
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)
92 #define DBGLVL_FW 4
93 #define DBGLVL_DVB 8
94 #define DBGLVL_I2C 16
95 #define DBGLVL_API 32
96 #define DBGLVL_CMD 64
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)
112 enum port_t {
113 SAA7164_MPEG_UNDEFINED = 0,
114 SAA7164_MPEG_DVB,
115 SAA7164_MPEG_ENCODER,
116 SAA7164_MPEG_VBI,
119 enum saa7164_i2c_bus_nr {
120 SAA7164_I2C_BUS_0 = 0,
121 SAA7164_I2C_BUS_1,
122 SAA7164_I2C_BUS_2,
125 enum saa7164_buffer_flags {
126 SAA7164_BUFFER_UNDEFINED = 0,
127 SAA7164_BUFFER_FREE,
128 SAA7164_BUFFER_BUSY,
129 SAA7164_BUFFER_FULL
132 enum saa7164_unit_type {
133 SAA7164_UNIT_UNDEFINED = 0,
134 SAA7164_UNIT_DIGITAL_DEMODULATOR,
135 SAA7164_UNIT_ANALOG_DEMODULATOR,
136 SAA7164_UNIT_TUNER,
137 SAA7164_UNIT_EEPROM,
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;
152 u8 id;
153 char *name;
154 enum saa7164_i2c_bus_nr i2c_bus_nr;
155 u8 i2c_bus_addr;
156 u8 i2c_reg_len;
159 struct saa7164_board {
160 char *name;
161 enum port_t porta, portb, portc,
162 portd, porte, portf;
163 enum {
164 SAA7164_CHIP_UNDEFINED = 0,
165 SAA7164_CHIP_REV2,
166 SAA7164_CHIP_REV3,
167 } chiprev;
168 struct saa7164_unit unit[SAA7164_MAX_UNITS];
171 struct saa7164_subid {
172 u16 subvendor;
173 u16 subdevice;
174 u32 card;
177 struct saa7164_encoder_fh {
178 struct v4l2_fh fh;
179 struct saa7164_port *port;
180 atomic_t v4l_reading;
183 struct saa7164_vbi_fh {
184 struct v4l2_fh fh;
185 struct saa7164_port *port;
186 atomic_t v4l_reading;
189 struct saa7164_histogram_bucket {
190 u32 val;
191 u32 count;
192 u64 update_time;
195 struct saa7164_histogram {
196 char name[32];
197 struct saa7164_histogram_bucket counter1[64];
200 struct saa7164_user_buffer {
201 struct list_head list;
203 /* Attributes */
204 u8 *data;
205 u32 pos;
206 u32 actual_size;
208 u32 crc;
211 struct saa7164_fw_status {
213 /* RISC Core details */
214 u32 status;
215 u32 mode;
216 u32 spec;
217 u32 inst;
218 u32 cpuload;
219 u32 remainheap;
221 /* Firmware version */
222 u32 version;
223 u32 major;
224 u32 sub;
225 u32 rel;
226 u32 buildnr;
229 struct saa7164_dvb {
230 struct mutex lock;
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;
237 struct dvb_net net;
238 int feeding;
241 struct saa7164_i2c {
242 struct saa7164_dev *dev;
244 enum saa7164_i2c_bus_nr nr;
246 /* I2C I/O */
247 struct i2c_adapter i2c_adap;
248 struct i2c_client i2c_client;
249 u32 i2c_rc;
252 struct saa7164_tvnorm {
253 char *name;
254 v4l2_std_id id;
257 struct saa7164_encoder_params {
258 struct saa7164_tvnorm encodernorm;
259 u32 height;
260 u32 width;
261 u32 is_50hz;
262 u32 bitrate; /* bps */
263 u32 bitrate_peak; /* bps */
264 u32 bitrate_mode;
265 u32 stream_type; /* V4L2_MPEG_STREAM_TYPE_MPEG2_TS */
267 u32 audio_sampling_freq;
268 u32 ctl_mute;
269 u32 ctl_aspect;
270 u32 refdist;
271 u32 gop_size;
274 struct saa7164_vbi_params {
275 struct saa7164_tvnorm encodernorm;
276 u32 height;
277 u32 width;
278 u32 is_50hz;
279 u32 bitrate; /* bps */
280 u32 bitrate_peak; /* bps */
281 u32 bitrate_mode;
282 u32 stream_type; /* V4L2_MPEG_STREAM_TYPE_MPEG2_TS */
284 u32 audio_sampling_freq;
285 u32 ctl_mute;
286 u32 ctl_aspect;
287 u32 refdist;
288 u32 gop_size;
291 struct saa7164_port;
293 struct saa7164_buffer {
294 struct list_head list;
296 /* Note of which h/w buffer list index position we occupy */
297 int idx;
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 */
316 /* Encoder fops */
317 u32 pos;
318 u32 actual_size;
321 struct saa7164_port {
323 struct saa7164_dev *dev;
324 enum port_t type;
325 int nr;
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 */
336 u32 bufcounter;
337 u32 pitch;
338 u32 bufsize;
339 u32 bufoffset;
340 u32 bufptr32l;
341 u32 bufptr32h;
342 u64 bufptr64;
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;
351 u32 last_svc_wp;
352 u32 last_svc_rp;
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 --- */
369 /* Encoder */
370 /* Defaults established in saa7164-encoder.c */
371 struct saa7164_tvnorm encodernorm;
372 struct v4l2_ctrl_handler ctrl_handler;
373 v4l2_std_id std;
374 u32 height;
375 u32 width;
376 u32 freq;
377 u8 mux_input;
378 u8 encoder_profile;
379 u8 video_format;
380 u8 audio_format;
381 u8 video_resolution;
382 u16 ctl_brightness;
383 u16 ctl_contrast;
384 u16 ctl_hue;
385 u16 ctl_saturation;
386 u16 ctl_sharpness;
387 s8 ctl_volume;
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;
406 /* V4L VBI */
407 struct tmComResVBIFormatDescrHeader vbi_fmt_ntsc;
408 struct saa7164_vbi_params vbi_params;
409 struct saa7164_port *enc_port;
411 /* Debug */
412 u32 sync_errors;
413 u32 v_cc_errors;
414 u32 a_cc_errors;
415 u8 last_v_cc;
416 u8 last_a_cc;
417 u32 done_first_interrupt;
420 struct saa7164_dev {
421 struct list_head devlist;
422 atomic_t refcount;
424 struct v4l2_device v4l2_dev;
426 /* pci stuff */
427 struct pci_dev *pci;
428 unsigned char pci_rev, pci_lat;
429 int pci_bus, pci_slot;
430 u32 __iomem *lmmio;
431 u8 __iomem *bmmio;
432 u32 __iomem *lmmio2;
433 u8 __iomem *bmmio2;
434 int pci_irqmask;
436 /* board details */
437 int nr;
438 int hwrevision;
439 u32 board;
440 char name[16];
442 /* firmware status */
443 struct saa7164_fw_status fw_status;
444 u32 firmwareloaded;
446 struct tmComResHWDescr hwdesc;
447 struct tmComResInterfaceDescr intfdesc;
448 struct tmComResBusDescr busdesc;
450 struct tmComResBusInfo bus;
452 /* Interrupt status and ack registers */
453 u32 int_status;
454 u32 int_ack;
455 bool msi;
457 struct cmd cmds[SAA_CMD_MAX_MSG_UNITS];
458 struct mutex lock;
460 /* I2c related */
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 /* ----------------------------------------------------------- */
482 /* saa7164-core.c */
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 /* ----------------------------------------------------------- */
489 /* saa7164-fw.c */
490 int saa7164_downloadfirmware(struct saa7164_dev *dev);
492 /* ----------------------------------------------------------- */
493 /* saa7164-i2c.c */
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 /* ----------------------------------------------------------- */
500 /* saa7164-bus.c */
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,
504 void *buf);
505 int saa7164_bus_get(struct saa7164_dev *dev, struct tmComResInfo* msg,
506 void *buf, int peekonly);
508 /* ----------------------------------------------------------- */
509 /* saa7164-cmd.c */
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 /* ----------------------------------------------------------- */
517 /* saa7164-api.c */
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 /* ----------------------------------------------------------- */
566 /* saa7164-dvb.c */
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 /* ----------------------------------------------------------- */
599 /* saa7164-vbi.c */
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);\
611 } while (0)
613 #define log_warn(fmt, arg...)\
614 do { \
615 printk(KERN_WARNING "%s: " fmt, dev->name, ## arg);\
616 } while (0)
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))