2 * Driver for the NXP SAA7164 PCIe bridge
4 * Copyright (c) 2009 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/i2c-algo-bit.h>
50 #include <linux/kdev_t.h>
52 #include <media/tuner.h>
53 #include <media/tveeprom.h>
54 #include <media/videobuf-dma-sg.h>
55 #include <media/videobuf-dvb.h>
57 #include "saa7164-reg.h"
58 #include "saa7164-types.h"
60 #include <linux/version.h>
61 #include <linux/mutex.h>
63 #define SAA7164_MAXBOARDS 8
66 #define SAA7164_BOARD_NOAUTO UNSET
67 #define SAA7164_BOARD_UNKNOWN 0
68 #define SAA7164_BOARD_UNKNOWN_REV2 1
69 #define SAA7164_BOARD_UNKNOWN_REV3 2
70 #define SAA7164_BOARD_HAUPPAUGE_HVR2250 3
71 #define SAA7164_BOARD_HAUPPAUGE_HVR2200 4
72 #define SAA7164_BOARD_HAUPPAUGE_HVR2200_2 5
73 #define SAA7164_BOARD_HAUPPAUGE_HVR2200_3 6
74 #define SAA7164_BOARD_HAUPPAUGE_HVR2250_2 7
75 #define SAA7164_BOARD_HAUPPAUGE_HVR2250_3 8
77 #define SAA7164_MAX_UNITS 8
78 #define SAA7164_TS_NUMBER_OF_LINES 312
79 #define SAA7164_PT_ENTRIES 16 /* (312 * 188) / 4096 */
86 #define DBGLVL_BUS 128
87 #define DBGLVL_IRQ 256
88 #define DBGLVL_BUF 512
91 SAA7164_MPEG_UNDEFINED
= 0,
95 enum saa7164_i2c_bus_nr
{
96 SAA7164_I2C_BUS_0
= 0,
101 enum saa7164_buffer_flags
{
102 SAA7164_BUFFER_UNDEFINED
= 0,
108 enum saa7164_unit_type
{
109 SAA7164_UNIT_UNDEFINED
= 0,
110 SAA7164_UNIT_DIGITAL_DEMODULATOR
,
111 SAA7164_UNIT_ANALOG_DEMODULATOR
,
114 SAA7164_UNIT_ZILOG_IRBLASTER
,
115 SAA7164_UNIT_ENCODER
,
118 /* The PCIe bridge doesn't grant direct access to i2c.
119 * Instead, you address i2c devices using a uniqely
120 * allocated 'unitid' value via a messaging API. This
121 * is a problem. The kernel and existing demod/tuner
122 * drivers expect to talk 'i2c', so we have to maintain
123 * a translation layer, and a series of functions to
124 * convert i2c bus + device address into a unit id.
126 struct saa7164_unit
{
127 enum saa7164_unit_type type
;
130 enum saa7164_i2c_bus_nr i2c_bus_nr
;
135 struct saa7164_board
{
137 enum port_t porta
, portb
;
139 SAA7164_CHIP_UNDEFINED
= 0,
143 struct saa7164_unit unit
[SAA7164_MAX_UNITS
];
146 struct saa7164_subid
{
152 struct saa7164_fw_status
{
154 /* RISC Core details */
162 /* Firmware version */
172 struct dvb_adapter adapter
;
173 struct dvb_frontend
*frontend
;
174 struct dvb_demux demux
;
175 struct dmxdev dmxdev
;
176 struct dmx_frontend fe_hw
;
177 struct dmx_frontend fe_mem
;
183 struct saa7164_dev
*dev
;
185 enum saa7164_i2c_bus_nr nr
;
188 struct i2c_adapter i2c_adap
;
189 struct i2c_algo_bit_data i2c_algo
;
190 struct i2c_client i2c_client
;
194 struct saa7164_tsport
;
196 struct saa7164_buffer
{
197 struct list_head list
;
201 struct saa7164_tsport
*port
;
203 /* Hardware Specific */
204 /* PCI Memory allocations */
205 enum saa7164_buffer_flags flags
; /* Free, Busy, Full */
207 /* A block of page align PCI memory */
208 u32 pci_size
; /* PCI allocation size in bytes */
209 u64
*cpu
; /* Virtual address */
210 dma_addr_t dma
; /* Physical address */
212 /* A page table that splits the block into a number of entries */
213 u32 pt_size
; /* PCI allocation size in bytes */
214 u64
*pt_cpu
; /* Virtual address */
215 dma_addr_t pt_dma
; /* Physical address */
218 struct saa7164_tsport
{
220 struct saa7164_dev
*dev
;
224 struct saa7164_dvb dvb
;
226 /* HW related stream parameters */
227 tmHWStreamParameters_t hw_streamingparams
;
229 /* DMA configuration values, is seeded during initialization */
230 tmComResDMATermDescrHeader_t hwcfg
;
232 /* hardware specific registers */
241 u32 numpte
; /* Number of entries in array, only valid in head */
242 struct mutex dmaqueue_lock
;
243 struct mutex dummy_dmaqueue_lock
;
244 struct saa7164_buffer dmaqueue
;
245 struct saa7164_buffer dummy_dmaqueue
;
250 struct list_head devlist
;
255 unsigned char pci_rev
, pci_lat
;
256 int pci_bus
, pci_slot
;
269 /* firmware status */
270 struct saa7164_fw_status fw_status
;
272 tmComResHWDescr_t hwdesc
;
273 tmComResInterfaceDescr_t intfdesc
;
274 tmComResBusDescr_t busdesc
;
276 tmComResBusInfo_t bus
;
278 /* Interrupt status and ack registers */
282 struct cmd cmds
[SAA_CMD_MAX_MSG_UNITS
];
286 struct saa7164_i2c i2c_bus
[3];
288 /* Transport related */
289 struct saa7164_tsport ts1
, ts2
;
291 /* Deferred command/api interrupts handling */
292 struct work_struct workcmd
;
296 extern struct list_head saa7164_devlist
;
297 extern unsigned int waitsecs
;
299 /* ----------------------------------------------------------- */
301 void saa7164_dumpregs(struct saa7164_dev
*dev
, u32 addr
);
302 void saa7164_dumphex16(struct saa7164_dev
*dev
, u8
*buf
, int len
);
303 void saa7164_getfirmwarestatus(struct saa7164_dev
*dev
);
304 u32
saa7164_getcurrentfirmwareversion(struct saa7164_dev
*dev
);
306 /* ----------------------------------------------------------- */
308 int saa7164_downloadfirmware(struct saa7164_dev
*dev
);
310 /* ----------------------------------------------------------- */
312 extern int saa7164_i2c_register(struct saa7164_i2c
*bus
);
313 extern int saa7164_i2c_unregister(struct saa7164_i2c
*bus
);
314 extern void saa7164_call_i2c_clients(struct saa7164_i2c
*bus
,
315 unsigned int cmd
, void *arg
);
317 /* ----------------------------------------------------------- */
319 int saa7164_bus_setup(struct saa7164_dev
*dev
);
320 void saa7164_bus_dump(struct saa7164_dev
*dev
);
321 int saa7164_bus_set(struct saa7164_dev
*dev
, tmComResInfo_t
* msg
, void *buf
);
322 int saa7164_bus_get(struct saa7164_dev
*dev
, tmComResInfo_t
* msg
,
323 void *buf
, int peekonly
);
325 /* ----------------------------------------------------------- */
327 int saa7164_cmd_send(struct saa7164_dev
*dev
,
328 u8 id
, tmComResCmd_t command
, u16 controlselector
,
329 u16 size
, void *buf
);
330 void saa7164_cmd_signal(struct saa7164_dev
*dev
, u8 seqno
);
331 int saa7164_irq_dequeue(struct saa7164_dev
*dev
);
333 /* ----------------------------------------------------------- */
335 int saa7164_api_get_fw_version(struct saa7164_dev
*dev
, u32
*version
);
336 int saa7164_api_enum_subdevs(struct saa7164_dev
*dev
);
337 int saa7164_api_i2c_read(struct saa7164_i2c
*bus
, u8 addr
, u32 reglen
, u8
*reg
,
338 u32 datalen
, u8
*data
);
339 int saa7164_api_i2c_write(struct saa7164_i2c
*bus
, u8 addr
,
340 u32 datalen
, u8
*data
);
341 int saa7164_api_dif_write(struct saa7164_i2c
*bus
, u8 addr
,
342 u32 datalen
, u8
*data
);
343 int saa7164_api_read_eeprom(struct saa7164_dev
*dev
, u8
*buf
, int buflen
);
344 int saa7164_api_set_gpiobit(struct saa7164_dev
*dev
, u8 unitid
, u8 pin
);
345 int saa7164_api_clear_gpiobit(struct saa7164_dev
*dev
, u8 unitid
, u8 pin
);
346 int saa7164_api_transition_port(struct saa7164_tsport
*port
, u8 mode
);
348 /* ----------------------------------------------------------- */
349 /* saa7164-cards.c */
350 extern struct saa7164_board saa7164_boards
[];
351 extern const unsigned int saa7164_bcount
;
353 extern struct saa7164_subid saa7164_subids
[];
354 extern const unsigned int saa7164_idcount
;
356 extern void saa7164_card_list(struct saa7164_dev
*dev
);
357 extern void saa7164_gpio_setup(struct saa7164_dev
*dev
);
358 extern void saa7164_card_setup(struct saa7164_dev
*dev
);
360 extern int saa7164_i2caddr_to_reglen(struct saa7164_i2c
*bus
, int addr
);
361 extern int saa7164_i2caddr_to_unitid(struct saa7164_i2c
*bus
, int addr
);
362 extern char *saa7164_unitid_name(struct saa7164_dev
*dev
, u8 unitid
);
364 /* ----------------------------------------------------------- */
366 extern int saa7164_dvb_register(struct saa7164_tsport
*port
);
367 extern int saa7164_dvb_unregister(struct saa7164_tsport
*port
);
369 /* ----------------------------------------------------------- */
370 /* saa7164-buffer.c */
371 extern struct saa7164_buffer
*saa7164_buffer_alloc(struct saa7164_tsport
*port
,
373 extern int saa7164_buffer_dealloc(struct saa7164_tsport
*port
,
374 struct saa7164_buffer
*buf
);
376 /* ----------------------------------------------------------- */
378 extern unsigned int saa_debug
;
379 #define dprintk(level, fmt, arg...)\
380 do { if (saa_debug & level)\
381 printk(KERN_DEBUG "%s: " fmt, dev->name, ## arg);\
384 #define log_warn(fmt, arg...)\
386 printk(KERN_WARNING "%s: " fmt, dev->name, ## arg);\
389 #define log_err(fmt, arg...)\
391 printk(KERN_ERROR "%s: " fmt, dev->name, ## arg);\
394 #define saa7164_readl(reg) readl(dev->lmmio + ((reg) >> 2))
395 #define saa7164_writel(reg, value) writel((value), dev->lmmio + ((reg) >> 2))
398 #define saa7164_readb(reg) readl(dev->bmmio + (reg))
399 #define saa7164_writeb(reg, value) writel((value), dev->bmmio + (reg))