blob: ad3907244020c0bef65e27b8073b338095c6da4a [file] [log] [blame]
Steven Toth443c12282009-05-09 21:17:28 -03001/*
2 * Driver for the NXP SAA7164 PCIe bridge
3 *
Steven Toth9b8b0192010-07-31 14:39:44 -03004 * Copyright (c) 2010 Steven Toth <stoth@kernellabs.com>
Steven Toth443c12282009-05-09 21:17:28 -03005 *
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.
10 *
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
14 *
15 * GNU General Public License for more details.
16 *
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.
20 */
21
22/*
23 Driver architecture
24 *******************
25
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.
31 V
32 saa7164_api..() : Translate kernel specific functions/features
33 | : into command buffers.
34 V
35 saa7164_cmd..() : Manages the flow of command packets on/off,
36 | : the bus. Deal with bus errors, timeouts etc.
37 V
38 saa7164_bus..() : Manage a read/write memory ring buffer in the
39 | : PCIe Address space.
40 |
41 | saa7164_fw...() : Load any frimware
42 | | : direct into the device
43 V V
44 <- ----------------- PCIe address space -------------------- ->
45*/
46
47#include <linux/pci.h>
48#include <linux/i2c.h>
49#include <linux/i2c-algo-bit.h>
50#include <linux/kdev_t.h>
Steven Toth7615e432010-07-31 14:44:53 -030051#include <linux/version.h>
52#include <linux/mutex.h>
Steven Toth443c12282009-05-09 21:17:28 -030053
54#include <media/tuner.h>
55#include <media/tveeprom.h>
56#include <media/videobuf-dma-sg.h>
57#include <media/videobuf-dvb.h>
Steven Toth7615e432010-07-31 14:44:53 -030058#include <linux/smp_lock.h>
59#include <dvb_demux.h>
60#include <dvb_frontend.h>
61#include <dvb_net.h>
62#include <dvbdev.h>
63#include <dmxdev.h>
64#include <media/v4l2-common.h>
65#include <media/v4l2-ioctl.h>
66#include <media/v4l2-chip-ident.h>
Steven Toth443c12282009-05-09 21:17:28 -030067
68#include "saa7164-reg.h"
69#include "saa7164-types.h"
70
Steven Toth443c12282009-05-09 21:17:28 -030071#define SAA7164_MAXBOARDS 8
72
73#define UNSET (-1U)
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
Steven Tothe3335222009-05-11 22:03:07 -030083#define SAA7164_BOARD_HAUPPAUGE_HVR2250_3 8
Steven Toth443c12282009-05-09 21:17:28 -030084
85#define SAA7164_MAX_UNITS 8
86#define SAA7164_TS_NUMBER_OF_LINES 312
87#define SAA7164_PT_ENTRIES 16 /* (312 * 188) / 4096 */
Steven Toth7615e432010-07-31 14:44:53 -030088#define SAA7164_MAX_ENCODER_BUFFERS 16
89
90/* Port related defines */
91#define SAA7164_PORT_TS1 (0)
92#define SAA7164_PORT_TS2 (SAA7164_PORT_TS1 + 1)
93#define SAA7164_PORT_ENC1 (SAA7164_PORT_TS2 + 1)
94#define SAA7164_PORT_ENC2 (SAA7164_PORT_ENC1 + 1)
95#define SAA7164_MAX_PORTS (SAA7164_PORT_ENC2 + 1)
Steven Toth443c12282009-05-09 21:17:28 -030096
97#define DBGLVL_FW 4
98#define DBGLVL_DVB 8
99#define DBGLVL_I2C 16
100#define DBGLVL_API 32
101#define DBGLVL_CMD 64
102#define DBGLVL_BUS 128
103#define DBGLVL_IRQ 256
104#define DBGLVL_BUF 512
Steven Toth9b8b0192010-07-31 14:39:44 -0300105#define DBGLVL_ENC 1024
Steven Toth443c12282009-05-09 21:17:28 -0300106
Steven Toth7615e432010-07-31 14:44:53 -0300107#define SAA7164_NORMS ( V4L2_STD_NTSC_M | V4L2_STD_NTSC_M_JP | V4L2_STD_NTSC_443 )
108
Steven Toth443c12282009-05-09 21:17:28 -0300109enum port_t {
110 SAA7164_MPEG_UNDEFINED = 0,
111 SAA7164_MPEG_DVB,
Steven Tothadd3f582010-07-31 14:43:07 -0300112 SAA7164_MPEG_ENCODER,
Steven Toth443c12282009-05-09 21:17:28 -0300113};
114
115enum saa7164_i2c_bus_nr {
116 SAA7164_I2C_BUS_0 = 0,
117 SAA7164_I2C_BUS_1,
118 SAA7164_I2C_BUS_2,
119};
120
121enum saa7164_buffer_flags {
122 SAA7164_BUFFER_UNDEFINED = 0,
123 SAA7164_BUFFER_FREE,
124 SAA7164_BUFFER_BUSY,
125 SAA7164_BUFFER_FULL
126};
127
128enum saa7164_unit_type {
129 SAA7164_UNIT_UNDEFINED = 0,
130 SAA7164_UNIT_DIGITAL_DEMODULATOR,
131 SAA7164_UNIT_ANALOG_DEMODULATOR,
132 SAA7164_UNIT_TUNER,
133 SAA7164_UNIT_EEPROM,
134 SAA7164_UNIT_ZILOG_IRBLASTER,
135 SAA7164_UNIT_ENCODER,
136};
137
138/* The PCIe bridge doesn't grant direct access to i2c.
139 * Instead, you address i2c devices using a uniqely
140 * allocated 'unitid' value via a messaging API. This
141 * is a problem. The kernel and existing demod/tuner
142 * drivers expect to talk 'i2c', so we have to maintain
143 * a translation layer, and a series of functions to
144 * convert i2c bus + device address into a unit id.
145 */
146struct saa7164_unit {
147 enum saa7164_unit_type type;
148 u8 id;
149 char *name;
150 enum saa7164_i2c_bus_nr i2c_bus_nr;
151 u8 i2c_bus_addr;
152 u8 i2c_reg_len;
153};
154
155struct saa7164_board {
156 char *name;
Steven Toth7615e432010-07-31 14:44:53 -0300157 enum port_t porta, portb, portc, portd;
Steven Toth443c12282009-05-09 21:17:28 -0300158 enum {
159 SAA7164_CHIP_UNDEFINED = 0,
160 SAA7164_CHIP_REV2,
161 SAA7164_CHIP_REV3,
162 } chiprev;
163 struct saa7164_unit unit[SAA7164_MAX_UNITS];
164};
165
166struct saa7164_subid {
167 u16 subvendor;
168 u16 subdevice;
169 u32 card;
170};
171
Steven Toth7615e432010-07-31 14:44:53 -0300172struct saa7164_fh {
173 struct saa7164_port *port;
174 u32 freq;
175 u32 tuner_type;
176 atomic_t v4l_reading;
177};
178
179struct saa7164_user_buffer {
180 struct list_head list;
181
182 /* Attributes */
183 u8 *data;
184 u32 pos;
185 u32 actual_size;
186};
187
Steven Toth443c12282009-05-09 21:17:28 -0300188struct saa7164_fw_status {
189
190 /* RISC Core details */
191 u32 status;
192 u32 mode;
193 u32 spec;
194 u32 inst;
195 u32 cpuload;
196 u32 remainheap;
197
198 /* Firmware version */
199 u32 version;
200 u32 major;
201 u32 sub;
202 u32 rel;
203 u32 buildnr;
204};
205
206struct saa7164_dvb {
207 struct mutex lock;
208 struct dvb_adapter adapter;
209 struct dvb_frontend *frontend;
210 struct dvb_demux demux;
211 struct dmxdev dmxdev;
212 struct dmx_frontend fe_hw;
213 struct dmx_frontend fe_mem;
214 struct dvb_net net;
215 int feeding;
216};
217
218struct saa7164_i2c {
219 struct saa7164_dev *dev;
220
221 enum saa7164_i2c_bus_nr nr;
222
223 /* I2C I/O */
224 struct i2c_adapter i2c_adap;
225 struct i2c_algo_bit_data i2c_algo;
226 struct i2c_client i2c_client;
227 u32 i2c_rc;
228};
229
Steven Toth7615e432010-07-31 14:44:53 -0300230struct saa7164_ctrl {
231 struct v4l2_queryctrl v;
232};
233
234struct saa7164_tvnorm {
235 char *name;
236 v4l2_std_id id;
237// u32 cxiformat;
238// u32 cxoformat;
239};
240
241struct saa7164_encoder_params {
242 struct saa7164_tvnorm encodernorm;
243 u32 height;
244 u32 width;
245 u32 is_50hz;
246 u32 bitrate; /* bps */
247 u32 stream_type; /* V4L2_MPEG_STREAM_TYPE_MPEG2_TS */
248
249 u32 audio_sampling_freq;
250 u32 ctl_mute;
251 u32 ctl_aspect;
252};
253
Steven Tothadd3f582010-07-31 14:43:07 -0300254struct saa7164_port;
Steven Toth443c12282009-05-09 21:17:28 -0300255
256struct saa7164_buffer {
257 struct list_head list;
258
Steven Tothadd3f582010-07-31 14:43:07 -0300259 /* Note of which h/w buffer list index position we occupy */
260 int idx;
Steven Toth443c12282009-05-09 21:17:28 -0300261
Steven Tothadd3f582010-07-31 14:43:07 -0300262 struct saa7164_port *port;
Steven Toth443c12282009-05-09 21:17:28 -0300263
264 /* Hardware Specific */
265 /* PCI Memory allocations */
266 enum saa7164_buffer_flags flags; /* Free, Busy, Full */
267
268 /* A block of page align PCI memory */
269 u32 pci_size; /* PCI allocation size in bytes */
270 u64 *cpu; /* Virtual address */
271 dma_addr_t dma; /* Physical address */
272
273 /* A page table that splits the block into a number of entries */
274 u32 pt_size; /* PCI allocation size in bytes */
275 u64 *pt_cpu; /* Virtual address */
276 dma_addr_t pt_dma; /* Physical address */
Steven Tothadd3f582010-07-31 14:43:07 -0300277
278 /* Encoder fops */
279 u32 pos;
280 u32 actual_size;
Steven Toth443c12282009-05-09 21:17:28 -0300281};
282
Steven Tothadd3f582010-07-31 14:43:07 -0300283struct saa7164_port {
Steven Toth443c12282009-05-09 21:17:28 -0300284
285 struct saa7164_dev *dev;
Steven Toth443c12282009-05-09 21:17:28 -0300286 enum port_t type;
Steven Tothadd3f582010-07-31 14:43:07 -0300287 int nr;
Steven Toth443c12282009-05-09 21:17:28 -0300288
Steven Tothadd3f582010-07-31 14:43:07 -0300289 /* --- Generic port attributes --- */
Steven Toth443c12282009-05-09 21:17:28 -0300290
Steven Tothadd3f582010-07-31 14:43:07 -0300291 /* HW stream parameters */
Steven Toth443c12282009-05-09 21:17:28 -0300292 tmHWStreamParameters_t hw_streamingparams;
293
294 /* DMA configuration values, is seeded during initialization */
295 tmComResDMATermDescrHeader_t hwcfg;
296
297 /* hardware specific registers */
298 u32 bufcounter;
299 u32 pitch;
300 u32 bufsize;
301 u32 bufoffset;
302 u32 bufptr32l;
303 u32 bufptr32h;
304 u64 bufptr64;
305
306 u32 numpte; /* Number of entries in array, only valid in head */
Steven Tothadd3f582010-07-31 14:43:07 -0300307
Steven Toth443c12282009-05-09 21:17:28 -0300308 struct mutex dmaqueue_lock;
Steven Toth443c12282009-05-09 21:17:28 -0300309 struct saa7164_buffer dmaqueue;
Steven Tothadd3f582010-07-31 14:43:07 -0300310
311 /* --- DVB Transport Specific --- */
312 struct saa7164_dvb dvb;
313
314 /* --- Encoder/V4L related attributes --- */
Steven Toth7615e432010-07-31 14:44:53 -0300315 /* Encoder */
316 /* Defaults established in saa7164-encoder.c */
317 struct saa7164_tvnorm encodernorm;
318 u32 height;
319 u32 width;
320 u32 freq;
321 u32 ts_packet_size;
322 u32 ts_packet_count;
323 u8 mux_input;
324 u8 encoder_profile;
325 u8 video_format;
326 u8 audio_format;
327 u8 video_resolution;
328 u16 ctl_brightness;
329 u16 ctl_contrast;
330 u16 ctl_hue;
331 u16 ctl_saturation;
332 u16 ctl_sharpness;
333 s8 ctl_volume;
Steven Toth443c12282009-05-09 21:17:28 -0300334
Steven Toth7615e432010-07-31 14:44:53 -0300335 tmComResAFeatureDescrHeader_t audfeat;
336 tmComResEncoderDescrHeader_t encunit;
337 tmComResProcDescrHeader_t vidproc;
338 tmComResExtDevDescrHeader_t ifunit;
339 tmComResTunerDescrHeader_t tunerunit;
340
341 /* V4L */
342 struct saa7164_encoder_params encoder_params;
343 struct video_device *v4l_device;
344 atomic_t v4l_reader_count;
345// spinlock_t slock;
346// struct mutex fops_lock;
347
348 struct saa7164_buffer list_buf_used;
349 struct saa7164_buffer list_buf_free;
350 wait_queue_head_t wait_read;
Steven Toth443c12282009-05-09 21:17:28 -0300351};
352
353struct saa7164_dev {
354 struct list_head devlist;
355 atomic_t refcount;
356
357 /* pci stuff */
358 struct pci_dev *pci;
359 unsigned char pci_rev, pci_lat;
360 int pci_bus, pci_slot;
361 u32 __iomem *lmmio;
362 u8 __iomem *bmmio;
363 u32 __iomem *lmmio2;
364 u8 __iomem *bmmio2;
365 int pci_irqmask;
366
367 /* board details */
368 int nr;
369 int hwrevision;
370 u32 board;
371 char name[32];
372
373 /* firmware status */
374 struct saa7164_fw_status fw_status;
375
376 tmComResHWDescr_t hwdesc;
377 tmComResInterfaceDescr_t intfdesc;
378 tmComResBusDescr_t busdesc;
379
380 tmComResBusInfo_t bus;
381
Steven Toth1a6450d2009-05-10 14:08:27 -0300382 /* Interrupt status and ack registers */
383 u32 int_status;
384 u32 int_ack;
Steven Toth443c12282009-05-09 21:17:28 -0300385
386 struct cmd cmds[SAA_CMD_MAX_MSG_UNITS];
387 struct mutex lock;
388
389 /* I2c related */
390 struct saa7164_i2c i2c_bus[3];
391
392 /* Transport related */
Steven Toth7615e432010-07-31 14:44:53 -0300393 struct saa7164_port ports[ SAA7164_MAX_PORTS ];
Steven Toth443c12282009-05-09 21:17:28 -0300394
395 /* Deferred command/api interrupts handling */
396 struct work_struct workcmd;
397
398};
399
400extern struct list_head saa7164_devlist;
Steven Tothdd1ee442009-07-30 09:09:30 -0300401extern unsigned int waitsecs;
Steven Toth443c12282009-05-09 21:17:28 -0300402
403/* ----------------------------------------------------------- */
404/* saa7164-core.c */
405void saa7164_dumpregs(struct saa7164_dev *dev, u32 addr);
406void saa7164_dumphex16(struct saa7164_dev *dev, u8 *buf, int len);
407void saa7164_getfirmwarestatus(struct saa7164_dev *dev);
408u32 saa7164_getcurrentfirmwareversion(struct saa7164_dev *dev);
409
410/* ----------------------------------------------------------- */
411/* saa7164-fw.c */
412int saa7164_downloadfirmware(struct saa7164_dev *dev);
413
414/* ----------------------------------------------------------- */
415/* saa7164-i2c.c */
416extern int saa7164_i2c_register(struct saa7164_i2c *bus);
417extern int saa7164_i2c_unregister(struct saa7164_i2c *bus);
418extern void saa7164_call_i2c_clients(struct saa7164_i2c *bus,
419 unsigned int cmd, void *arg);
420
421/* ----------------------------------------------------------- */
422/* saa7164-bus.c */
423int saa7164_bus_setup(struct saa7164_dev *dev);
424void saa7164_bus_dump(struct saa7164_dev *dev);
425int saa7164_bus_set(struct saa7164_dev *dev, tmComResInfo_t* msg, void *buf);
426int saa7164_bus_get(struct saa7164_dev *dev, tmComResInfo_t* msg,
427 void *buf, int peekonly);
428
429/* ----------------------------------------------------------- */
430/* saa7164-cmd.c */
431int saa7164_cmd_send(struct saa7164_dev *dev,
432 u8 id, tmComResCmd_t command, u16 controlselector,
433 u16 size, void *buf);
434void saa7164_cmd_signal(struct saa7164_dev *dev, u8 seqno);
Steven Toth39e469a2009-08-12 12:14:37 -0300435int saa7164_irq_dequeue(struct saa7164_dev *dev);
Steven Toth443c12282009-05-09 21:17:28 -0300436
437/* ----------------------------------------------------------- */
438/* saa7164-api.c */
Steven Toth443c12282009-05-09 21:17:28 -0300439int saa7164_api_get_fw_version(struct saa7164_dev *dev, u32 *version);
440int saa7164_api_enum_subdevs(struct saa7164_dev *dev);
441int saa7164_api_i2c_read(struct saa7164_i2c *bus, u8 addr, u32 reglen, u8 *reg,
442 u32 datalen, u8 *data);
443int saa7164_api_i2c_write(struct saa7164_i2c *bus, u8 addr,
444 u32 datalen, u8 *data);
445int saa7164_api_dif_write(struct saa7164_i2c *bus, u8 addr,
446 u32 datalen, u8 *data);
447int saa7164_api_read_eeprom(struct saa7164_dev *dev, u8 *buf, int buflen);
448int saa7164_api_set_gpiobit(struct saa7164_dev *dev, u8 unitid, u8 pin);
449int saa7164_api_clear_gpiobit(struct saa7164_dev *dev, u8 unitid, u8 pin);
Steven Tothadd3f582010-07-31 14:43:07 -0300450int saa7164_api_transition_port(struct saa7164_port *port, u8 mode);
Steven Toth7615e432010-07-31 14:44:53 -0300451int saa7164_api_initialize_dif(struct saa7164_port *port);
452int saa7164_api_configure_dif(struct saa7164_port *port, u32 std);
453int saa7164_api_set_encoder(struct saa7164_port *port);
454int saa7164_api_get_encoder(struct saa7164_port *port);
455int saa7164_api_set_aspect_ratio(struct saa7164_port *port);
456int saa7164_api_set_usercontrol(struct saa7164_port *port, u8 ctl);
457int saa7164_api_get_usercontrol(struct saa7164_port *port, u8 ctl);
458int saa7164_api_set_videomux(struct saa7164_port *port);
459int saa7164_api_audio_mute(struct saa7164_port *port, int mute);
460int saa7164_api_set_audio_volume(struct saa7164_port *port, s8 level);
461int saa7164_api_set_audio_std(struct saa7164_port *port);
462int saa7164_api_set_audio_detection(struct saa7164_port *port, int autodetect);
463int saa7164_api_get_videomux(struct saa7164_port *port);
Steven Toth443c12282009-05-09 21:17:28 -0300464
465/* ----------------------------------------------------------- */
466/* saa7164-cards.c */
467extern struct saa7164_board saa7164_boards[];
468extern const unsigned int saa7164_bcount;
469
470extern struct saa7164_subid saa7164_subids[];
471extern const unsigned int saa7164_idcount;
472
473extern void saa7164_card_list(struct saa7164_dev *dev);
474extern void saa7164_gpio_setup(struct saa7164_dev *dev);
475extern void saa7164_card_setup(struct saa7164_dev *dev);
476
477extern int saa7164_i2caddr_to_reglen(struct saa7164_i2c *bus, int addr);
478extern int saa7164_i2caddr_to_unitid(struct saa7164_i2c *bus, int addr);
479extern char *saa7164_unitid_name(struct saa7164_dev *dev, u8 unitid);
480
481/* ----------------------------------------------------------- */
482/* saa7164-dvb.c */
Steven Tothadd3f582010-07-31 14:43:07 -0300483extern int saa7164_dvb_register(struct saa7164_port *port);
484extern int saa7164_dvb_unregister(struct saa7164_port *port);
Steven Toth443c12282009-05-09 21:17:28 -0300485
486/* ----------------------------------------------------------- */
487/* saa7164-buffer.c */
Steven Tothadd3f582010-07-31 14:43:07 -0300488extern struct saa7164_buffer *saa7164_buffer_alloc(
489 struct saa7164_port *port, u32 len);
490extern int saa7164_buffer_dealloc(struct saa7164_buffer *buf);
491extern void saa7164_buffer_display(struct saa7164_buffer *buf);
492extern int saa7164_buffer_activate(struct saa7164_buffer *buf, int i);
493extern int saa7164_buffer_cfg_port(struct saa7164_port *port);
Steven Toth7615e432010-07-31 14:44:53 -0300494extern struct saa7164_user_buffer *saa7164_buffer_alloc_user(
495 struct saa7164_dev *dev, u32 len);
496extern void saa7164_buffer_dealloc_user(struct saa7164_user_buffer *buf);
497
498
499/* ----------------------------------------------------------- */
500/* saa7164-encoder.c */
501int saa7164_encoder_register(struct saa7164_port *port);
502void saa7164_encoder_unregister(struct saa7164_port *port);
Steven Toth443c12282009-05-09 21:17:28 -0300503
504/* ----------------------------------------------------------- */
505
Ingo Molnarb1912a852009-09-21 15:23:45 -0300506extern unsigned int saa_debug;
Steven Toth443c12282009-05-09 21:17:28 -0300507#define dprintk(level, fmt, arg...)\
Ingo Molnarb1912a852009-09-21 15:23:45 -0300508 do { if (saa_debug & level)\
Steven Toth443c12282009-05-09 21:17:28 -0300509 printk(KERN_DEBUG "%s: " fmt, dev->name, ## arg);\
510 } while (0)
511
512#define log_warn(fmt, arg...)\
513 do { \
514 printk(KERN_WARNING "%s: " fmt, dev->name, ## arg);\
515 } while (0)
516
517#define log_err(fmt, arg...)\
518 do { \
519 printk(KERN_ERROR "%s: " fmt, dev->name, ## arg);\
520 } while (0)
521
522#define saa7164_readl(reg) readl(dev->lmmio + ((reg) >> 2))
Steven Toth207b42c2009-05-09 21:30:05 -0300523#define saa7164_writel(reg, value) writel((value), dev->lmmio + ((reg) >> 2))
524
Steven Toth443c12282009-05-09 21:17:28 -0300525
526#define saa7164_readb(reg) readl(dev->bmmio + (reg))
527#define saa7164_writeb(reg, value) writel((value), dev->bmmio + (reg))
528