V4L/DVB (13329): s5h1409: provide HVR-1600 specific optimizations
[safe/jmp/linux-2.6] / drivers / media / video / cx18 / cx18-dvb.c
1 /*
2  *  cx18 functions for DVB support
3  *
4  *  Copyright (c) 2008 Steven Toth <stoth@linuxtv.org>
5  *  Copyright (C) 2008  Andy Walls <awalls@radix.net>
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation; either version 2 of the License, or
10  *  (at your option) any later version.
11  *
12  *  This program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program; if not, write to the Free Software
20  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  */
22
23 #include "cx18-version.h"
24 #include "cx18-dvb.h"
25 #include "cx18-io.h"
26 #include "cx18-queue.h"
27 #include "cx18-streams.h"
28 #include "cx18-cards.h"
29 #include "cx18-gpio.h"
30 #include "s5h1409.h"
31 #include "mxl5005s.h"
32 #include "zl10353.h"
33
34 #include <linux/firmware.h>
35 #include "mt352.h"
36 #include "mt352_priv.h"
37 #include "tuner-xc2028.h"
38
39 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
40
41 #define CX18_REG_DMUX_NUM_PORT_0_CONTROL 0xd5a000
42 #define CX18_CLOCK_ENABLE2               0xc71024
43 #define CX18_DMUX_CLK_MASK               0x0080
44
45 /*
46  * CX18_CARD_HVR_1600_ESMT
47  * CX18_CARD_HVR_1600_SAMSUNG
48  */
49
50 static struct mxl5005s_config hauppauge_hvr1600_tuner = {
51         .i2c_address     = 0xC6 >> 1,
52         .if_freq         = IF_FREQ_5380000HZ,
53         .xtal_freq       = CRYSTAL_FREQ_16000000HZ,
54         .agc_mode        = MXL_SINGLE_AGC,
55         .tracking_filter = MXL_TF_C_H,
56         .rssi_enable     = MXL_RSSI_ENABLE,
57         .cap_select      = MXL_CAP_SEL_ENABLE,
58         .div_out         = MXL_DIV_OUT_4,
59         .clock_out       = MXL_CLOCK_OUT_DISABLE,
60         .output_load     = MXL5005S_IF_OUTPUT_LOAD_200_OHM,
61         .top             = MXL5005S_TOP_25P2,
62         .mod_mode        = MXL_DIGITAL_MODE,
63         .if_mode         = MXL_ZERO_IF,
64         .AgcMasterByte   = 0x00,
65 };
66
67 static struct s5h1409_config hauppauge_hvr1600_config = {
68         .demod_address = 0x32 >> 1,
69         .output_mode   = S5H1409_SERIAL_OUTPUT,
70         .gpio          = S5H1409_GPIO_ON,
71         .qam_if        = 44000,
72         .inversion     = S5H1409_INVERSION_OFF,
73         .status_mode   = S5H1409_DEMODLOCKING,
74         .mpeg_timing   = S5H1409_MPEGTIMING_CONTINOUS_NONINVERTING_CLOCK,
75         .hvr1600_opt   = S5H1409_HVR1600_OPTIMIZE
76 };
77
78 /*
79  * CX18_CARD_LEADTEK_DVR3100H
80  */
81 /* Information/confirmation of proper config values provided by Terry Wu */
82 static struct zl10353_config leadtek_dvr3100h_demod = {
83         .demod_address         = 0x1e >> 1, /* Datasheet suggested straps */
84         .if2                   = 45600,     /* 4.560 MHz IF from the XC3028 */
85         .parallel_ts           = 1,         /* Not a serial TS */
86         .no_tuner              = 1,         /* XC3028 is not behind the gate */
87         .disable_i2c_gate_ctrl = 1,         /* Disable the I2C gate */
88 };
89
90 /*
91  * CX18_CARD_YUAN_MPC718
92  */
93 /*
94  * Due to
95  *
96  * 1. an absence of information on how to prgram the MT352
97  * 2. the Linux mt352 module pushing MT352 initialzation off onto us here
98  *
99  * We have to use an init sequence that *you* must extract from the Windows
100  * driver (yuanrap.sys) and which we load as a firmware.
101  *
102  * If someone can provide me with a Zarlink MT352 (Intel CE6352?) Design Manual
103  * with chip programming details, then I can remove this annoyance.
104  */
105 static int yuan_mpc718_mt352_reqfw(struct cx18_stream *stream,
106                                    const struct firmware **fw)
107 {
108         struct cx18 *cx = stream->cx;
109         const char *fn = "dvb-cx18-mpc718-mt352.fw";
110         int ret;
111
112         ret = request_firmware(fw, fn, &cx->pci_dev->dev);
113         if (ret)
114                 CX18_ERR("Unable to open firmware file %s\n", fn);
115         else {
116                 size_t sz = (*fw)->size;
117                 if (sz < 2 || sz > 64 || (sz % 2) != 0) {
118                         CX18_ERR("Firmware %s has a bad size: %lu bytes\n",
119                                  fn, (unsigned long) sz);
120                         ret = -EILSEQ;
121                         release_firmware(*fw);
122                         *fw = NULL;
123                 }
124         }
125
126         if (ret) {
127                 CX18_ERR("The MPC718 board variant with the MT352 DVB-T"
128                           "demodualtor will not work without it\n");
129                 CX18_ERR("Run 'linux/Documentation/dvb/get_dvb_firmware "
130                           "mpc718' if you need the firmware\n");
131         }
132         return ret;
133 }
134
135 static int yuan_mpc718_mt352_init(struct dvb_frontend *fe)
136 {
137         struct cx18_dvb *dvb = container_of(fe->dvb,
138                                             struct cx18_dvb, dvb_adapter);
139         struct cx18_stream *stream = container_of(dvb, struct cx18_stream, dvb);
140         const struct firmware *fw = NULL;
141         int ret;
142         int i;
143         u8 buf[3];
144
145         ret = yuan_mpc718_mt352_reqfw(stream, &fw);
146         if (ret)
147                 return ret;
148
149         /* Loop through all the register-value pairs in the firmware file */
150         for (i = 0; i < fw->size; i += 2) {
151                 buf[0] = fw->data[i];
152                 /* Intercept a few registers we want to set ourselves */
153                 switch (buf[0]) {
154                 case TRL_NOMINAL_RATE_0:
155                         /* Set our custom OFDM bandwidth in the case below */
156                         break;
157                 case TRL_NOMINAL_RATE_1:
158                         /* 6 MHz: 64/7 * 6/8 / 20.48 * 2^16 = 0x55b6.db6 */
159                         /* 7 MHz: 64/7 * 7/8 / 20.48 * 2^16 = 0x6400 */
160                         /* 8 MHz: 64/7 * 8/8 / 20.48 * 2^16 = 0x7249.249 */
161                         buf[1] = 0x72;
162                         buf[2] = 0x49;
163                         mt352_write(fe, buf, 3);
164                         break;
165                 case INPUT_FREQ_0:
166                         /* Set our custom IF in the case below */
167                         break;
168                 case INPUT_FREQ_1:
169                         /* 4.56 MHz IF: (20.48 - 4.56)/20.48 * 2^14 = 0x31c0 */
170                         buf[1] = 0x31;
171                         buf[2] = 0xc0;
172                         mt352_write(fe, buf, 3);
173                         break;
174                 default:
175                         /* Pass through the register-value pair from the fw */
176                         buf[1] = fw->data[i+1];
177                         mt352_write(fe, buf, 2);
178                         break;
179                 }
180         }
181
182         buf[0] = (u8) TUNER_GO;
183         buf[1] = 0x01; /* Go */
184         mt352_write(fe, buf, 2);
185         release_firmware(fw);
186         return 0;
187 }
188
189 static struct mt352_config yuan_mpc718_mt352_demod = {
190         .demod_address = 0x1e >> 1,
191         .adc_clock     = 20480,     /* 20.480 MHz */
192         .if2           =  4560,     /*  4.560 MHz */
193         .no_tuner      = 1,         /* XC3028 is not behind the gate */
194         .demod_init    = yuan_mpc718_mt352_init,
195 };
196
197 static struct zl10353_config yuan_mpc718_zl10353_demod = {
198         .demod_address         = 0x1e >> 1, /* Datasheet suggested straps */
199         .if2                   = 45600,     /* 4.560 MHz IF from the XC3028 */
200         .parallel_ts           = 1,         /* Not a serial TS */
201         .no_tuner              = 1,         /* XC3028 is not behind the gate */
202         .disable_i2c_gate_ctrl = 1,         /* Disable the I2C gate */
203 };
204
205 static int dvb_register(struct cx18_stream *stream);
206
207 /* Kernel DVB framework calls this when the feed needs to start.
208  * The CX18 framework should enable the transport DMA handling
209  * and queue processing.
210  */
211 static int cx18_dvb_start_feed(struct dvb_demux_feed *feed)
212 {
213         struct dvb_demux *demux = feed->demux;
214         struct cx18_stream *stream = (struct cx18_stream *) demux->priv;
215         struct cx18 *cx = stream->cx;
216         int ret;
217         u32 v;
218
219         CX18_DEBUG_INFO("Start feed: pid = 0x%x index = %d\n",
220                         feed->pid, feed->index);
221
222         mutex_lock(&cx->serialize_lock);
223         ret = cx18_init_on_first_open(cx);
224         mutex_unlock(&cx->serialize_lock);
225         if (ret) {
226                 CX18_ERR("Failed to initialize firmware starting DVB feed\n");
227                 return ret;
228         }
229         ret = -EINVAL;
230
231         switch (cx->card->type) {
232         case CX18_CARD_HVR_1600_ESMT:
233         case CX18_CARD_HVR_1600_SAMSUNG:
234                 v = cx18_read_reg(cx, CX18_REG_DMUX_NUM_PORT_0_CONTROL);
235                 v |= 0x00400000; /* Serial Mode */
236                 v |= 0x00002000; /* Data Length - Byte */
237                 v |= 0x00010000; /* Error - Polarity */
238                 v |= 0x00020000; /* Error - Passthru */
239                 v |= 0x000c0000; /* Error - Ignore */
240                 cx18_write_reg(cx, v, CX18_REG_DMUX_NUM_PORT_0_CONTROL);
241                 break;
242
243         case CX18_CARD_LEADTEK_DVR3100H:
244         case CX18_CARD_YUAN_MPC718:
245         default:
246                 /* Assumption - Parallel transport - Signalling
247                  * undefined or default.
248                  */
249                 break;
250         }
251
252         if (!demux->dmx.frontend)
253                 return -EINVAL;
254
255         if (!stream)
256                 return -EINVAL;
257
258         mutex_lock(&stream->dvb.feedlock);
259         if (stream->dvb.feeding++ == 0) {
260                 CX18_DEBUG_INFO("Starting Transport DMA\n");
261                 set_bit(CX18_F_S_STREAMING, &stream->s_flags);
262                 ret = cx18_start_v4l2_encode_stream(stream);
263                 if (ret < 0) {
264                         CX18_DEBUG_INFO("Failed to start Transport DMA\n");
265                         stream->dvb.feeding--;
266                         if (stream->dvb.feeding == 0)
267                                 clear_bit(CX18_F_S_STREAMING, &stream->s_flags);
268                 }
269         } else
270                 ret = 0;
271         mutex_unlock(&stream->dvb.feedlock);
272
273         return ret;
274 }
275
276 /* Kernel DVB framework calls this when the feed needs to stop. */
277 static int cx18_dvb_stop_feed(struct dvb_demux_feed *feed)
278 {
279         struct dvb_demux *demux = feed->demux;
280         struct cx18_stream *stream = (struct cx18_stream *)demux->priv;
281         struct cx18 *cx = stream->cx;
282         int ret = -EINVAL;
283
284         CX18_DEBUG_INFO("Stop feed: pid = 0x%x index = %d\n",
285                         feed->pid, feed->index);
286
287         if (stream) {
288                 mutex_lock(&stream->dvb.feedlock);
289                 if (--stream->dvb.feeding == 0) {
290                         CX18_DEBUG_INFO("Stopping Transport DMA\n");
291                         ret = cx18_stop_v4l2_encode_stream(stream, 0);
292                 } else
293                         ret = 0;
294                 mutex_unlock(&stream->dvb.feedlock);
295         }
296
297         return ret;
298 }
299
300 int cx18_dvb_register(struct cx18_stream *stream)
301 {
302         struct cx18 *cx = stream->cx;
303         struct cx18_dvb *dvb = &stream->dvb;
304         struct dvb_adapter *dvb_adapter;
305         struct dvb_demux *dvbdemux;
306         struct dmx_demux *dmx;
307         int ret;
308
309         if (!dvb)
310                 return -EINVAL;
311
312         ret = dvb_register_adapter(&dvb->dvb_adapter,
313                         CX18_DRIVER_NAME,
314                         THIS_MODULE, &cx->pci_dev->dev, adapter_nr);
315         if (ret < 0)
316                 goto err_out;
317
318         dvb_adapter = &dvb->dvb_adapter;
319
320         dvbdemux = &dvb->demux;
321
322         dvbdemux->priv = (void *)stream;
323
324         dvbdemux->filternum = 256;
325         dvbdemux->feednum = 256;
326         dvbdemux->start_feed = cx18_dvb_start_feed;
327         dvbdemux->stop_feed = cx18_dvb_stop_feed;
328         dvbdemux->dmx.capabilities = (DMX_TS_FILTERING |
329                 DMX_SECTION_FILTERING | DMX_MEMORY_BASED_FILTERING);
330         ret = dvb_dmx_init(dvbdemux);
331         if (ret < 0)
332                 goto err_dvb_unregister_adapter;
333
334         dmx = &dvbdemux->dmx;
335
336         dvb->hw_frontend.source = DMX_FRONTEND_0;
337         dvb->mem_frontend.source = DMX_MEMORY_FE;
338         dvb->dmxdev.filternum = 256;
339         dvb->dmxdev.demux = dmx;
340
341         ret = dvb_dmxdev_init(&dvb->dmxdev, dvb_adapter);
342         if (ret < 0)
343                 goto err_dvb_dmx_release;
344
345         ret = dmx->add_frontend(dmx, &dvb->hw_frontend);
346         if (ret < 0)
347                 goto err_dvb_dmxdev_release;
348
349         ret = dmx->add_frontend(dmx, &dvb->mem_frontend);
350         if (ret < 0)
351                 goto err_remove_hw_frontend;
352
353         ret = dmx->connect_frontend(dmx, &dvb->hw_frontend);
354         if (ret < 0)
355                 goto err_remove_mem_frontend;
356
357         ret = dvb_register(stream);
358         if (ret < 0)
359                 goto err_disconnect_frontend;
360
361         dvb_net_init(dvb_adapter, &dvb->dvbnet, dmx);
362
363         CX18_INFO("DVB Frontend registered\n");
364         CX18_INFO("Registered DVB adapter%d for %s (%d x %d kB)\n",
365                   stream->dvb.dvb_adapter.num, stream->name,
366                   stream->buffers, stream->buf_size/1024);
367
368         mutex_init(&dvb->feedlock);
369         dvb->enabled = 1;
370         return ret;
371
372 err_disconnect_frontend:
373         dmx->disconnect_frontend(dmx);
374 err_remove_mem_frontend:
375         dmx->remove_frontend(dmx, &dvb->mem_frontend);
376 err_remove_hw_frontend:
377         dmx->remove_frontend(dmx, &dvb->hw_frontend);
378 err_dvb_dmxdev_release:
379         dvb_dmxdev_release(&dvb->dmxdev);
380 err_dvb_dmx_release:
381         dvb_dmx_release(dvbdemux);
382 err_dvb_unregister_adapter:
383         dvb_unregister_adapter(dvb_adapter);
384 err_out:
385         return ret;
386 }
387
388 void cx18_dvb_unregister(struct cx18_stream *stream)
389 {
390         struct cx18 *cx = stream->cx;
391         struct cx18_dvb *dvb = &stream->dvb;
392         struct dvb_adapter *dvb_adapter;
393         struct dvb_demux *dvbdemux;
394         struct dmx_demux *dmx;
395
396         CX18_INFO("unregister DVB\n");
397
398         dvb_adapter = &dvb->dvb_adapter;
399         dvbdemux = &dvb->demux;
400         dmx = &dvbdemux->dmx;
401
402         dmx->close(dmx);
403         dvb_net_release(&dvb->dvbnet);
404         dmx->remove_frontend(dmx, &dvb->mem_frontend);
405         dmx->remove_frontend(dmx, &dvb->hw_frontend);
406         dvb_dmxdev_release(&dvb->dmxdev);
407         dvb_dmx_release(dvbdemux);
408         dvb_unregister_frontend(dvb->fe);
409         dvb_frontend_detach(dvb->fe);
410         dvb_unregister_adapter(dvb_adapter);
411 }
412
413 /* All the DVB attach calls go here, this function get's modified
414  * for each new card. cx18_dvb_start_feed() will also need changes.
415  */
416 static int dvb_register(struct cx18_stream *stream)
417 {
418         struct cx18_dvb *dvb = &stream->dvb;
419         struct cx18 *cx = stream->cx;
420         int ret = 0;
421
422         switch (cx->card->type) {
423         case CX18_CARD_HVR_1600_ESMT:
424         case CX18_CARD_HVR_1600_SAMSUNG:
425                 dvb->fe = dvb_attach(s5h1409_attach,
426                         &hauppauge_hvr1600_config,
427                         &cx->i2c_adap[0]);
428                 if (dvb->fe != NULL) {
429                         dvb_attach(mxl5005s_attach, dvb->fe,
430                                 &cx->i2c_adap[0],
431                                 &hauppauge_hvr1600_tuner);
432                         ret = 0;
433                 }
434                 break;
435         case CX18_CARD_LEADTEK_DVR3100H:
436                 dvb->fe = dvb_attach(zl10353_attach,
437                                      &leadtek_dvr3100h_demod,
438                                      &cx->i2c_adap[1]);
439                 if (dvb->fe != NULL) {
440                         struct dvb_frontend *fe;
441                         struct xc2028_config cfg = {
442                                 .i2c_adap = &cx->i2c_adap[1],
443                                 .i2c_addr = 0xc2 >> 1,
444                                 .ctrl = NULL,
445                         };
446                         static struct xc2028_ctrl ctrl = {
447                                 .fname   = XC2028_DEFAULT_FIRMWARE,
448                                 .max_len = 64,
449                                 .demod   = XC3028_FE_ZARLINK456,
450                                 .type    = XC2028_AUTO,
451                         };
452
453                         fe = dvb_attach(xc2028_attach, dvb->fe, &cfg);
454                         if (fe != NULL && fe->ops.tuner_ops.set_config != NULL)
455                                 fe->ops.tuner_ops.set_config(fe, &ctrl);
456                 }
457                 break;
458         case CX18_CARD_YUAN_MPC718:
459                 /*
460                  * TODO
461                  * Apparently, these cards also could instead have a
462                  * DiBcom demod supported by one of the db7000 drivers
463                  */
464                 dvb->fe = dvb_attach(mt352_attach,
465                                      &yuan_mpc718_mt352_demod,
466                                      &cx->i2c_adap[1]);
467                 if (dvb->fe == NULL)
468                         dvb->fe = dvb_attach(zl10353_attach,
469                                              &yuan_mpc718_zl10353_demod,
470                                              &cx->i2c_adap[1]);
471                 if (dvb->fe != NULL) {
472                         struct dvb_frontend *fe;
473                         struct xc2028_config cfg = {
474                                 .i2c_adap = &cx->i2c_adap[1],
475                                 .i2c_addr = 0xc2 >> 1,
476                                 .ctrl = NULL,
477                         };
478                         static struct xc2028_ctrl ctrl = {
479                                 .fname   = XC2028_DEFAULT_FIRMWARE,
480                                 .max_len = 64,
481                                 .demod   = XC3028_FE_ZARLINK456,
482                                 .type    = XC2028_AUTO,
483                         };
484
485                         fe = dvb_attach(xc2028_attach, dvb->fe, &cfg);
486                         if (fe != NULL && fe->ops.tuner_ops.set_config != NULL)
487                                 fe->ops.tuner_ops.set_config(fe, &ctrl);
488                 }
489                 break;
490         default:
491                 /* No Digital Tv Support */
492                 break;
493         }
494
495         if (dvb->fe == NULL) {
496                 CX18_ERR("frontend initialization failed\n");
497                 return -1;
498         }
499
500         dvb->fe->callback = cx18_reset_tuner_gpio;
501
502         ret = dvb_register_frontend(&dvb->dvb_adapter, dvb->fe);
503         if (ret < 0) {
504                 if (dvb->fe->ops.release)
505                         dvb->fe->ops.release(dvb->fe);
506                 return ret;
507         }
508
509         /*
510          * The firmware seems to enable the TS DMUX clock
511          * under various circumstances.  However, since we know we
512          * might use it, let's just turn it on ourselves here.
513          */
514         cx18_write_reg_expect(cx,
515                               (CX18_DMUX_CLK_MASK << 16) | CX18_DMUX_CLK_MASK,
516                               CX18_CLOCK_ENABLE2,
517                               CX18_DMUX_CLK_MASK,
518                               (CX18_DMUX_CLK_MASK << 16) | CX18_DMUX_CLK_MASK);
519
520         return ret;
521 }