V4L/DVB (6898): cx23885: add support for Hauppauge WinTV HVR-1500
[pandora-kernel.git] / drivers / media / video / cx23885 / cx23885-dvb.c
1 /*
2  *  Driver for the Conexant CX23885 PCIe bridge
3  *
4  *  Copyright (c) 2006 Steven Toth <stoth@hauppauge.com>
5  *
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 #include <linux/module.h>
23 #include <linux/init.h>
24 #include <linux/device.h>
25 #include <linux/fs.h>
26 #include <linux/kthread.h>
27 #include <linux/file.h>
28 #include <linux/suspend.h>
29
30 #include "cx23885.h"
31 #include <media/v4l2-common.h>
32
33 #include "s5h1409.h"
34 #include "mt2131.h"
35 #include "lgdt330x.h"
36 #include "xc5000.h"
37 #include "dvb-pll.h"
38 #include "tuner-xc2028.h"
39 #include "tuner-xc2028-types.h"
40
41 static unsigned int debug = 0;
42
43 #define dprintk(level,fmt, arg...)      if (debug >= level) \
44         printk(KERN_DEBUG "%s: " fmt, dev->name, ## arg)
45
46 /* ------------------------------------------------------------------ */
47
48 static int dvb_buf_setup(struct videobuf_queue *q,
49                          unsigned int *count, unsigned int *size)
50 {
51         struct cx23885_tsport *port = q->priv_data;
52
53         port->ts_packet_size  = 188 * 4;
54         port->ts_packet_count = 32;
55
56         *size  = port->ts_packet_size * port->ts_packet_count;
57         *count = 32;
58         return 0;
59 }
60
61 static int dvb_buf_prepare(struct videobuf_queue *q,
62                            struct videobuf_buffer *vb, enum v4l2_field field)
63 {
64         struct cx23885_tsport *port = q->priv_data;
65         return cx23885_buf_prepare(q, port, (struct cx23885_buffer*)vb, field);
66 }
67
68 static void dvb_buf_queue(struct videobuf_queue *q, struct videobuf_buffer *vb)
69 {
70         struct cx23885_tsport *port = q->priv_data;
71         cx23885_buf_queue(port, (struct cx23885_buffer*)vb);
72 }
73
74 static void dvb_buf_release(struct videobuf_queue *q,
75                             struct videobuf_buffer *vb)
76 {
77         cx23885_free_buffer(q, (struct cx23885_buffer*)vb);
78 }
79
80 static int cx23885_request_firmware(struct dvb_frontend *fe,
81         const struct firmware **fw, char *name)
82 {
83         struct cx23885_tsport *port = fe->dvb->priv;
84         struct cx23885_dev *dev = port->dev;
85
86         dprintk(1, "%s(?,?,%s)\n", __FUNCTION__, name);
87
88         return request_firmware(fw, name, &dev->pci->dev);
89 }
90
91 static int hauppauge_hvr1500q_tuner_reset(struct dvb_frontend *fe)
92 {
93         struct cx23885_tsport *port = fe->dvb->priv;
94         struct cx23885_dev *dev = port->dev;
95
96         dprintk(1, "%s()\n", __FUNCTION__);
97
98         /* Drive the tuner into reset back back */
99         cx_clear(GP0_IO, 0x00000004);
100         mdelay(200);
101         cx_set(GP0_IO, 0x00000004);
102
103         return 0;
104 }
105
106 static struct videobuf_queue_ops dvb_qops = {
107         .buf_setup    = dvb_buf_setup,
108         .buf_prepare  = dvb_buf_prepare,
109         .buf_queue    = dvb_buf_queue,
110         .buf_release  = dvb_buf_release,
111 };
112
113 static struct s5h1409_config hauppauge_generic_config = {
114         .demod_address = 0x32 >> 1,
115         .output_mode   = S5H1409_SERIAL_OUTPUT,
116         .gpio          = S5H1409_GPIO_ON,
117         .qam_if        = 44000,
118         .inversion     = S5H1409_INVERSION_OFF,
119         .status_mode   = S5H1409_DEMODLOCKING
120 };
121
122 static struct s5h1409_config hauppauge_hvr1800lp_config = {
123         .demod_address = 0x32 >> 1,
124         .output_mode   = S5H1409_SERIAL_OUTPUT,
125         .gpio          = S5H1409_GPIO_OFF,
126         .qam_if        = 44000,
127         .inversion     = S5H1409_INVERSION_OFF,
128         .status_mode   = S5H1409_DEMODLOCKING
129 };
130
131 static struct s5h1409_config hauppauge_hvr1500_config = {
132         .demod_address = 0x32 >> 1,
133         .output_mode   = S5H1409_SERIAL_OUTPUT,
134         .gpio          = S5H1409_GPIO_OFF,
135         .inversion     = S5H1409_INVERSION_OFF,
136         .status_mode   = S5H1409_DEMODLOCKING
137 };
138
139 static struct mt2131_config hauppauge_generic_tunerconfig = {
140         0x61
141 };
142
143 static struct lgdt330x_config fusionhdtv_5_express = {
144         .demod_address = 0x0e,
145         .demod_chip = LGDT3303,
146         .serial_mpeg = 0x40,
147 };
148
149 static struct s5h1409_config hauppauge_hvr1500q_config = {
150         .demod_address = 0x32 >> 1,
151         .output_mode   = S5H1409_SERIAL_OUTPUT,
152         .gpio          = S5H1409_GPIO_ON,
153         .qam_if        = 44000,
154         .inversion     = S5H1409_INVERSION_OFF,
155         .status_mode   = S5H1409_DEMODLOCKING
156 };
157
158 static struct xc5000_config hauppauge_hvr1500q_tunerconfig = {
159         .i2c_address      = 0x61,
160         .if_khz           = 5380,
161         .request_firmware = cx23885_request_firmware,
162         .tuner_reset      = hauppauge_hvr1500q_tuner_reset
163 };
164
165 static int cx23885_hvr1500_xc3028_callback(void *ptr, int command, int arg)
166 {
167         struct cx23885_tsport *port = ptr;
168         struct cx23885_dev *dev = port->dev;
169
170         switch (command) {
171         case XC2028_TUNER_RESET:
172                 /* Send the tuner in then out of reset */
173                 /* GPIO-2 xc3028 tuner */
174                 dprintk(1, "%s: XC2028_TUNER_RESET %d\n", __FUNCTION__, arg);
175
176                 cx_set(GP0_IO, 0x00040000);
177                 cx_clear(GP0_IO, 0x00000004);
178                 msleep(5);
179
180                 cx_set(GP0_IO, 0x00040004);
181                 msleep(5);
182                 break;
183         case XC2028_RESET_CLK:
184                 dprintk(1, "%s: XC2028_RESET_CLK %d\n", __FUNCTION__, arg);
185                 break;
186         default:
187                 dprintk(1, "%s: unknown command %d, arg %d\n", __FUNCTION__,
188                         command, arg);
189                 return -EINVAL;
190         }
191
192         return 0;
193 }
194
195 static int dvb_register(struct cx23885_tsport *port)
196 {
197         struct cx23885_dev *dev = port->dev;
198         struct cx23885_i2c *i2c_bus = NULL;
199
200         /* init struct videobuf_dvb */
201         port->dvb.name = dev->name;
202
203         /* init frontend */
204         switch (dev->board) {
205         case CX23885_BOARD_HAUPPAUGE_HVR1250:
206         case CX23885_BOARD_HAUPPAUGE_HVR1800:
207                 i2c_bus = &dev->i2c_bus[0];
208                 port->dvb.frontend = dvb_attach(s5h1409_attach,
209                                                 &hauppauge_generic_config,
210                                                 &i2c_bus->i2c_adap);
211                 if (port->dvb.frontend != NULL) {
212                         dvb_attach(mt2131_attach, port->dvb.frontend,
213                                    &i2c_bus->i2c_adap,
214                                    &hauppauge_generic_tunerconfig, 0);
215                 }
216                 break;
217         case CX23885_BOARD_HAUPPAUGE_HVR1800lp:
218                 i2c_bus = &dev->i2c_bus[0];
219                 port->dvb.frontend = dvb_attach(s5h1409_attach,
220                                                 &hauppauge_hvr1800lp_config,
221                                                 &i2c_bus->i2c_adap);
222                 if (port->dvb.frontend != NULL) {
223                         dvb_attach(mt2131_attach, port->dvb.frontend,
224                                    &i2c_bus->i2c_adap,
225                                    &hauppauge_generic_tunerconfig, 0);
226                 }
227                 break;
228         case CX23885_BOARD_DVICO_FUSIONHDTV_5_EXP:
229                 i2c_bus = &dev->i2c_bus[0];
230                 port->dvb.frontend = dvb_attach(lgdt330x_attach,
231                                                 &fusionhdtv_5_express,
232                                                 &i2c_bus->i2c_adap);
233                 if (port->dvb.frontend != NULL) {
234                         dvb_attach(dvb_pll_attach, port->dvb.frontend, 0x61,
235                                    &i2c_bus->i2c_adap, DVB_PLL_LG_TDVS_H06XF);
236                 }
237                 break;
238         case CX23885_BOARD_HAUPPAUGE_HVR1500Q:
239                 i2c_bus = &dev->i2c_bus[1];
240                 port->dvb.frontend = dvb_attach(s5h1409_attach,
241                                                 &hauppauge_hvr1500q_config,
242                                                 &dev->i2c_bus[0].i2c_adap);
243                 if (port->dvb.frontend != NULL) {
244                         dvb_attach(xc5000_attach, port->dvb.frontend,
245                                 &i2c_bus->i2c_adap,
246                                 &hauppauge_hvr1500q_tunerconfig);
247                 }
248                 break;
249         case CX23885_BOARD_HAUPPAUGE_HVR1500:
250                 i2c_bus = &dev->i2c_bus[1];
251                 port->dvb.frontend = dvb_attach(s5h1409_attach,
252                                                 &hauppauge_hvr1500_config,
253                                                 &dev->i2c_bus[0].i2c_adap);
254                 if (port->dvb.frontend != NULL) {
255                         struct dvb_frontend *fe;
256                         struct xc2028_config cfg = {
257                                 .i2c_adap  = &i2c_bus->i2c_adap,
258                                 .i2c_addr  = 0x61,
259                                 .video_dev = port,
260                                 .callback  = cx23885_hvr1500_xc3028_callback,
261                         };
262                         static struct xc2028_ctrl ctl = {
263                                 .fname       = "xc3028-v27.fw",
264                                 .max_len     = 64,
265                                 .scode_table = OREN538,
266                         };
267
268                         fe = dvb_attach(xc2028_attach,
269                                         port->dvb.frontend, &cfg);
270                         if (fe != NULL && fe->ops.tuner_ops.set_config != NULL)
271                                 fe->ops.tuner_ops.set_config(fe, &ctl);
272                 }
273                 break;
274         default:
275                 printk("%s: The frontend of your DVB/ATSC card isn't supported yet\n",
276                        dev->name);
277                 break;
278         }
279         if (NULL == port->dvb.frontend) {
280                 printk("%s: frontend initialization failed\n", dev->name);
281                 return -1;
282         }
283
284         /* Put the analog decoder in standby to keep it quiet */
285         cx23885_call_i2c_clients(i2c_bus, TUNER_SET_STANDBY, NULL);
286
287         /* register everything */
288         return videobuf_dvb_register(&port->dvb, THIS_MODULE, port,
289                                      &dev->pci->dev);
290 }
291
292 int cx23885_dvb_register(struct cx23885_tsport *port)
293 {
294         struct cx23885_dev *dev = port->dev;
295         int err;
296
297         dprintk(1, "%s\n", __FUNCTION__);
298         dprintk(1, " ->being probed by Card=%d Name=%s, PCI %02x:%02x\n",
299                 dev->board,
300                 dev->name,
301                 dev->pci_bus,
302                 dev->pci_slot);
303
304         err = -ENODEV;
305
306         /* dvb stuff */
307         printk("%s: cx23885 based dvb card\n", dev->name);
308         videobuf_queue_pci_init(&port->dvb.dvbq, &dvb_qops, dev->pci, &port->slock,
309                             V4L2_BUF_TYPE_VIDEO_CAPTURE, V4L2_FIELD_TOP,
310                             sizeof(struct cx23885_buffer), port);
311         err = dvb_register(port);
312         if (err != 0)
313                 printk("%s() dvb_register failed err = %d\n", __FUNCTION__, err);
314
315         return err;
316 }
317
318 int cx23885_dvb_unregister(struct cx23885_tsport *port)
319 {
320         /* dvb */
321         if(port->dvb.frontend)
322                 videobuf_dvb_unregister(&port->dvb);
323
324         return 0;
325 }
326
327 /*
328  * Local variables:
329  * c-basic-offset: 8
330  * End:
331  * kate: eol "unix"; indent-width 3; remove-trailing-space on; replace-trailing-space-save on; tab-width 8; replace-tabs off; space-indent off; mixed-indent off
332 */