xref: /linux/drivers/media/pci/pt3/pt3.h (revision 87c9c16317882dd6dbbc07e349bc3223e14f3244)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Earthsoft PT3 driver
4  *
5  * Copyright (C) 2014 Akihiro Tsukada <tskd08@gmail.com>
6  */
7 
8 #ifndef PT3_H
9 #define PT3_H
10 
11 #include <linux/atomic.h>
12 #include <linux/types.h>
13 
14 #include <media/dvb_demux.h>
15 #include <media/dvb_frontend.h>
16 #include <media/dmxdev.h>
17 
18 #include "tc90522.h"
19 #include "mxl301rf.h"
20 #include "qm1d1c0042.h"
21 
22 #define DRV_NAME KBUILD_MODNAME
23 
24 #define PT3_NUM_FE 4
25 
26 /*
27  * register index of the FPGA chip
28  */
29 #define REG_VERSION	0x00
30 #define REG_BUS		0x04
31 #define REG_SYSTEM_W	0x08
32 #define REG_SYSTEM_R	0x0c
33 #define REG_I2C_W	0x10
34 #define REG_I2C_R	0x14
35 #define REG_RAM_W	0x18
36 #define REG_RAM_R	0x1c
37 #define REG_DMA_BASE	0x40	/* regs for FE[i] = REG_DMA_BASE + 0x18 * i */
38 #define OFST_DMA_DESC_L	0x00
39 #define OFST_DMA_DESC_H	0x04
40 #define OFST_DMA_CTL	0x08
41 #define OFST_TS_CTL	0x0c
42 #define OFST_STATUS	0x10
43 #define OFST_TS_ERR	0x14
44 
45 /*
46  * internal buffer for I2C
47  */
48 #define PT3_I2C_MAX 4091
49 struct pt3_i2cbuf {
50 	u8  data[PT3_I2C_MAX];
51 	u8  tmp;
52 	u32 num_cmds;
53 };
54 
55 /*
56  * DMA things
57  */
58 #define TS_PACKET_SZ  188
59 /* DMA transfers must not cross 4GiB, so use one page / transfer */
60 #define DATA_XFER_SZ   4096
61 #define DATA_BUF_XFERS 47
62 /* (num_bufs * DATA_BUF_SZ) % TS_PACKET_SZ must be 0 */
63 #define DATA_BUF_SZ    (DATA_BUF_XFERS * DATA_XFER_SZ)
64 #define MAX_DATA_BUFS  16
65 #define MIN_DATA_BUFS   2
66 
67 #define DESCS_IN_PAGE (PAGE_SIZE / sizeof(struct xfer_desc))
68 #define MAX_NUM_XFERS (MAX_DATA_BUFS * DATA_BUF_XFERS)
69 #define MAX_DESC_BUFS DIV_ROUND_UP(MAX_NUM_XFERS, DESCS_IN_PAGE)
70 
71 /* DMA transfer description.
72  * device is passed a pointer to this struct, dma-reads it,
73  * and gets the DMA buffer ring for storing TS data.
74  */
75 struct xfer_desc {
76 	u32 addr_l; /* bus address of target data buffer */
77 	u32 addr_h;
78 	u32 size;
79 	u32 next_l; /* bus address of the next xfer_desc */
80 	u32 next_h;
81 };
82 
83 /* A DMA mapping of a page containing xfer_desc's */
84 struct xfer_desc_buffer {
85 	dma_addr_t b_addr;
86 	struct xfer_desc *descs; /* PAGE_SIZE (xfer_desc[DESCS_IN_PAGE]) */
87 };
88 
89 /* A DMA mapping of a data buffer */
90 struct dma_data_buffer {
91 	dma_addr_t b_addr;
92 	u8 *data; /* size: u8[PAGE_SIZE] */
93 };
94 
95 /*
96  * device things
97  */
98 struct pt3_adap_config {
99 	struct i2c_board_info demod_info;
100 	struct tc90522_config demod_cfg;
101 
102 	struct i2c_board_info tuner_info;
103 	union tuner_config {
104 		struct qm1d1c0042_config qm1d1c0042;
105 		struct mxl301rf_config   mxl301rf;
106 	} tuner_cfg;
107 	u32 init_freq;
108 };
109 
110 struct pt3_adapter {
111 	struct dvb_adapter  dvb_adap;  /* dvb_adap.priv => struct pt3_board */
112 	int adap_idx;
113 
114 	struct dvb_demux    demux;
115 	struct dmxdev       dmxdev;
116 	struct dvb_frontend *fe;
117 	struct i2c_client   *i2c_demod;
118 	struct i2c_client   *i2c_tuner;
119 
120 	/* data fetch thread */
121 	struct task_struct *thread;
122 	int num_feeds;
123 
124 	bool cur_lna;
125 	bool cur_lnb; /* current LNB power status (on/off) */
126 
127 	/* items below are for DMA */
128 	struct dma_data_buffer buffer[MAX_DATA_BUFS];
129 	int buf_idx;
130 	int buf_ofs;
131 	int num_bufs;  /* == pt3_board->num_bufs */
132 	int num_discard; /* how many access units to discard initially */
133 
134 	struct xfer_desc_buffer desc_buf[MAX_DESC_BUFS];
135 	int num_desc_bufs;  /* == num_bufs * DATA_BUF_XFERS / DESCS_IN_PAGE */
136 };
137 
138 
139 struct pt3_board {
140 	struct pci_dev *pdev;
141 	void __iomem *regs[2];
142 	/* regs[0]: registers, regs[1]: internal memory, used for I2C */
143 
144 	struct mutex lock;
145 
146 	/* LNB power shared among sat-FEs */
147 	int lnb_on_cnt; /* LNB power on count */
148 
149 	/* LNA shared among terr-FEs */
150 	int lna_on_cnt; /* booster enabled count */
151 
152 	int num_bufs;  /* number of DMA buffers allocated/mapped per FE */
153 
154 	struct i2c_adapter i2c_adap;
155 	struct pt3_i2cbuf *i2c_buf;
156 
157 	struct pt3_adapter *adaps[PT3_NUM_FE];
158 };
159 
160 
161 /*
162  * prototypes
163  */
164 extern int  pt3_alloc_dmabuf(struct pt3_adapter *adap);
165 extern void pt3_init_dmabuf(struct pt3_adapter *adap);
166 extern void pt3_free_dmabuf(struct pt3_adapter *adap);
167 extern int  pt3_start_dma(struct pt3_adapter *adap);
168 extern int  pt3_stop_dma(struct pt3_adapter *adap);
169 extern int  pt3_proc_dma(struct pt3_adapter *adap);
170 
171 extern int  pt3_i2c_master_xfer(struct i2c_adapter *adap,
172 				struct i2c_msg *msgs, int num);
173 extern u32  pt3_i2c_functionality(struct i2c_adapter *adap);
174 extern void pt3_i2c_reset(struct pt3_board *pt3);
175 extern int  pt3_init_all_demods(struct pt3_board *pt3);
176 extern int  pt3_init_all_mxl301rf(struct pt3_board *pt3);
177 #endif /* PT3_H */
178