1 /*
2  *  Driver for the NXP SAA7164 PCIe bridge
3  *
4  *  Copyright (c) 2010 Steven Toth <stoth@kernellabs.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 "saa7164.h"
23 
24 #include "tda10048.h"
25 #include "tda18271.h"
26 #include "s5h1411.h"
27 
28 #define DRIVER_NAME "saa7164"
29 
30 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
31 
32 /* addr is in the card struct, get it from there */
33 static struct tda10048_config hauppauge_hvr2200_1_config = {
34 	.demod_address    = 0x10 >> 1,
35 	.output_mode      = TDA10048_SERIAL_OUTPUT,
36 	.fwbulkwritelen   = TDA10048_BULKWRITE_200,
37 	.inversion        = TDA10048_INVERSION_ON,
38 	.dtv6_if_freq_khz = TDA10048_IF_3300,
39 	.dtv7_if_freq_khz = TDA10048_IF_3500,
40 	.dtv8_if_freq_khz = TDA10048_IF_4000,
41 	.clk_freq_khz     = TDA10048_CLK_16000,
42 };
43 static struct tda10048_config hauppauge_hvr2200_2_config = {
44 	.demod_address    = 0x12 >> 1,
45 	.output_mode      = TDA10048_SERIAL_OUTPUT,
46 	.fwbulkwritelen   = TDA10048_BULKWRITE_200,
47 	.inversion        = TDA10048_INVERSION_ON,
48 	.dtv6_if_freq_khz = TDA10048_IF_3300,
49 	.dtv7_if_freq_khz = TDA10048_IF_3500,
50 	.dtv8_if_freq_khz = TDA10048_IF_4000,
51 	.clk_freq_khz     = TDA10048_CLK_16000,
52 };
53 
54 static struct tda18271_std_map hauppauge_tda18271_std_map = {
55 	.atsc_6   = { .if_freq = 3250, .agc_mode = 3, .std = 3,
56 		      .if_lvl = 6, .rfagc_top = 0x37 },
57 	.qam_6    = { .if_freq = 4000, .agc_mode = 3, .std = 0,
58 		      .if_lvl = 6, .rfagc_top = 0x37 },
59 };
60 
61 static struct tda18271_config hauppauge_hvr22x0_tuner_config = {
62 	.std_map	= &hauppauge_tda18271_std_map,
63 	.gate		= TDA18271_GATE_ANALOG,
64 	.role		= TDA18271_MASTER,
65 };
66 
67 static struct tda18271_config hauppauge_hvr22x0s_tuner_config = {
68 	.std_map	= &hauppauge_tda18271_std_map,
69 	.gate		= TDA18271_GATE_ANALOG,
70 	.role		= TDA18271_SLAVE,
71 	.output_opt     = TDA18271_OUTPUT_LT_OFF,
72 	.rf_cal_on_startup = 1
73 };
74 
75 static struct s5h1411_config hauppauge_s5h1411_config = {
76 	.output_mode   = S5H1411_SERIAL_OUTPUT,
77 	.gpio          = S5H1411_GPIO_ON,
78 	.qam_if        = S5H1411_IF_4000,
79 	.vsb_if        = S5H1411_IF_3250,
80 	.inversion     = S5H1411_INVERSION_ON,
81 	.status_mode   = S5H1411_DEMODLOCKING,
82 	.mpeg_timing   = S5H1411_MPEGTIMING_CONTINOUS_NONINVERTING_CLOCK,
83 };
84 
85 static int saa7164_dvb_stop_port(struct saa7164_port *port)
86 {
87 	struct saa7164_dev *dev = port->dev;
88 	int ret;
89 
90 	ret = saa7164_api_transition_port(port, SAA_DMASTATE_STOP);
91 	if ((ret != SAA_OK) && (ret != SAA_ERR_ALREADY_STOPPED)) {
92 		printk(KERN_ERR "%s() stop transition failed, ret = 0x%x\n",
93 			__func__, ret);
94 		ret = -EIO;
95 	} else {
96 		dprintk(DBGLVL_DVB, "%s()    Stopped\n", __func__);
97 		ret = 0;
98 	}
99 
100 	return ret;
101 }
102 
103 static int saa7164_dvb_acquire_port(struct saa7164_port *port)
104 {
105 	struct saa7164_dev *dev = port->dev;
106 	int ret;
107 
108 	ret = saa7164_api_transition_port(port, SAA_DMASTATE_ACQUIRE);
109 	if ((ret != SAA_OK) && (ret != SAA_ERR_ALREADY_STOPPED)) {
110 		printk(KERN_ERR "%s() acquire transition failed, ret = 0x%x\n",
111 			__func__, ret);
112 		ret = -EIO;
113 	} else {
114 		dprintk(DBGLVL_DVB, "%s() Acquired\n", __func__);
115 		ret = 0;
116 	}
117 
118 	return ret;
119 }
120 
121 static int saa7164_dvb_pause_port(struct saa7164_port *port)
122 {
123 	struct saa7164_dev *dev = port->dev;
124 	int ret;
125 
126 	ret = saa7164_api_transition_port(port, SAA_DMASTATE_PAUSE);
127 	if ((ret != SAA_OK) && (ret != SAA_ERR_ALREADY_STOPPED)) {
128 		printk(KERN_ERR "%s() pause transition failed, ret = 0x%x\n",
129 			__func__, ret);
130 		ret = -EIO;
131 	} else {
132 		dprintk(DBGLVL_DVB, "%s()   Paused\n", __func__);
133 		ret = 0;
134 	}
135 
136 	return ret;
137 }
138 
139 /* Firmware is very windows centric, meaning you have to transition
140  * the part through AVStream / KS Windows stages, forwards or backwards.
141  * States are: stopped, acquired (h/w), paused, started.
142  */
143 static int saa7164_dvb_stop_streaming(struct saa7164_port *port)
144 {
145 	struct saa7164_dev *dev = port->dev;
146 	struct saa7164_buffer *buf;
147 	struct list_head *p, *q;
148 	int ret;
149 
150 	dprintk(DBGLVL_DVB, "%s(port=%d)\n", __func__, port->nr);
151 
152 	ret = saa7164_dvb_pause_port(port);
153 	ret = saa7164_dvb_acquire_port(port);
154 	ret = saa7164_dvb_stop_port(port);
155 
156 	/* Mark the hardware buffers as free */
157 	mutex_lock(&port->dmaqueue_lock);
158 	list_for_each_safe(p, q, &port->dmaqueue.list) {
159 		buf = list_entry(p, struct saa7164_buffer, list);
160 		buf->flags = SAA7164_BUFFER_FREE;
161 	}
162 	mutex_unlock(&port->dmaqueue_lock);
163 
164 	return ret;
165 }
166 
167 static int saa7164_dvb_start_port(struct saa7164_port *port)
168 {
169 	struct saa7164_dev *dev = port->dev;
170 	int ret = 0, result;
171 
172 	dprintk(DBGLVL_DVB, "%s(port=%d)\n", __func__, port->nr);
173 
174 	saa7164_buffer_cfg_port(port);
175 
176 	/* Acquire the hardware */
177 	result = saa7164_api_transition_port(port, SAA_DMASTATE_ACQUIRE);
178 	if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) {
179 		printk(KERN_ERR "%s() acquire transition failed, res = 0x%x\n",
180 			__func__, result);
181 
182 		/* Stop the hardware, regardless */
183 		result = saa7164_api_transition_port(port, SAA_DMASTATE_STOP);
184 		if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) {
185 			printk(KERN_ERR "%s() acquire/forced stop transition "
186 				"failed, res = 0x%x\n", __func__, result);
187 		}
188 		ret = -EIO;
189 		goto out;
190 	} else
191 		dprintk(DBGLVL_DVB, "%s()   Acquired\n", __func__);
192 
193 	/* Pause the hardware */
194 	result = saa7164_api_transition_port(port, SAA_DMASTATE_PAUSE);
195 	if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) {
196 		printk(KERN_ERR "%s() pause transition failed, res = 0x%x\n",
197 				__func__, result);
198 
199 		/* Stop the hardware, regardless */
200 		result = saa7164_api_transition_port(port, SAA_DMASTATE_STOP);
201 		if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) {
202 			printk(KERN_ERR "%s() pause/forced stop transition "
203 				"failed, res = 0x%x\n", __func__, result);
204 		}
205 
206 		ret = -EIO;
207 		goto out;
208 	} else
209 		dprintk(DBGLVL_DVB, "%s()   Paused\n", __func__);
210 
211 	/* Start the hardware */
212 	result = saa7164_api_transition_port(port, SAA_DMASTATE_RUN);
213 	if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) {
214 		printk(KERN_ERR "%s() run transition failed, result = 0x%x\n",
215 				__func__, result);
216 
217 		/* Stop the hardware, regardless */
218 		result = saa7164_api_transition_port(port, SAA_DMASTATE_STOP);
219 		if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) {
220 			printk(KERN_ERR "%s() run/forced stop transition "
221 				"failed, res = 0x%x\n", __func__, result);
222 		}
223 
224 		ret = -EIO;
225 	} else
226 		dprintk(DBGLVL_DVB, "%s()   Running\n", __func__);
227 
228 out:
229 	return ret;
230 }
231 
232 static int saa7164_dvb_start_feed(struct dvb_demux_feed *feed)
233 {
234 	struct dvb_demux *demux = feed->demux;
235 	struct saa7164_port *port = (struct saa7164_port *) demux->priv;
236 	struct saa7164_dvb *dvb = &port->dvb;
237 	struct saa7164_dev *dev = port->dev;
238 	int ret = 0;
239 
240 	dprintk(DBGLVL_DVB, "%s(port=%d)\n", __func__, port->nr);
241 
242 	if (!demux->dmx.frontend)
243 		return -EINVAL;
244 
245 	mutex_lock(&dvb->lock);
246 	if (dvb->feeding++ == 0) {
247 		/* Start transport */
248 		ret = saa7164_dvb_start_port(port);
249 	}
250 	mutex_unlock(&dvb->lock);
251 	dprintk(DBGLVL_DVB, "%s(port=%d) now feeding = %d\n",
252 		__func__, port->nr, dvb->feeding);
253 
254 	return ret;
255 }
256 
257 static int saa7164_dvb_stop_feed(struct dvb_demux_feed *feed)
258 {
259 	struct dvb_demux *demux = feed->demux;
260 	struct saa7164_port *port = (struct saa7164_port *) demux->priv;
261 	struct saa7164_dvb *dvb = &port->dvb;
262 	struct saa7164_dev *dev = port->dev;
263 	int ret = 0;
264 
265 	dprintk(DBGLVL_DVB, "%s(port=%d)\n", __func__, port->nr);
266 
267 	mutex_lock(&dvb->lock);
268 	if (--dvb->feeding == 0) {
269 		/* Stop transport */
270 		ret = saa7164_dvb_stop_streaming(port);
271 	}
272 	mutex_unlock(&dvb->lock);
273 	dprintk(DBGLVL_DVB, "%s(port=%d) now feeding = %d\n",
274 		__func__, port->nr, dvb->feeding);
275 
276 	return ret;
277 }
278 
279 static int dvb_register(struct saa7164_port *port)
280 {
281 	struct saa7164_dvb *dvb = &port->dvb;
282 	struct saa7164_dev *dev = port->dev;
283 	struct saa7164_buffer *buf;
284 	int result, i;
285 
286 	dprintk(DBGLVL_DVB, "%s(port=%d)\n", __func__, port->nr);
287 
288 	if (port->type != SAA7164_MPEG_DVB)
289 		BUG();
290 
291 	/* Sanity check that the PCI configuration space is active */
292 	if (port->hwcfg.BARLocation == 0) {
293 		result = -ENOMEM;
294 		printk(KERN_ERR "%s: dvb_register_adapter failed "
295 		       "(errno = %d), NO PCI configuration\n",
296 			DRIVER_NAME, result);
297 		goto fail_adapter;
298 	}
299 
300 	/* Init and establish defaults */
301 	port->hw_streamingparams.bitspersample = 8;
302 	port->hw_streamingparams.samplesperline = 188;
303 	port->hw_streamingparams.numberoflines =
304 		(SAA7164_TS_NUMBER_OF_LINES * 188) / 188;
305 
306 	port->hw_streamingparams.pitch = 188;
307 	port->hw_streamingparams.linethreshold = 0;
308 	port->hw_streamingparams.pagetablelistvirt = NULL;
309 	port->hw_streamingparams.pagetablelistphys = NULL;
310 	port->hw_streamingparams.numpagetables = 2 +
311 		((SAA7164_TS_NUMBER_OF_LINES * 188) / PAGE_SIZE);
312 
313 	port->hw_streamingparams.numpagetableentries = port->hwcfg.buffercount;
314 
315 	/* Allocate the PCI resources */
316 	for (i = 0; i < port->hwcfg.buffercount; i++) {
317 		buf = saa7164_buffer_alloc(port,
318 			port->hw_streamingparams.numberoflines *
319 			port->hw_streamingparams.pitch);
320 
321 		if (!buf) {
322 			result = -ENOMEM;
323 			printk(KERN_ERR "%s: dvb_register_adapter failed "
324 			       "(errno = %d), unable to allocate buffers\n",
325 				DRIVER_NAME, result);
326 			goto fail_adapter;
327 		}
328 
329 		mutex_lock(&port->dmaqueue_lock);
330 		list_add_tail(&buf->list, &port->dmaqueue.list);
331 		mutex_unlock(&port->dmaqueue_lock);
332 	}
333 
334 	/* register adapter */
335 	result = dvb_register_adapter(&dvb->adapter, DRIVER_NAME, THIS_MODULE,
336 			&dev->pci->dev, adapter_nr);
337 	if (result < 0) {
338 		printk(KERN_ERR "%s: dvb_register_adapter failed "
339 		       "(errno = %d)\n", DRIVER_NAME, result);
340 		goto fail_adapter;
341 	}
342 	dvb->adapter.priv = port;
343 
344 	/* register frontend */
345 	result = dvb_register_frontend(&dvb->adapter, dvb->frontend);
346 	if (result < 0) {
347 		printk(KERN_ERR "%s: dvb_register_frontend failed "
348 		       "(errno = %d)\n", DRIVER_NAME, result);
349 		goto fail_frontend;
350 	}
351 
352 	/* register demux stuff */
353 	dvb->demux.dmx.capabilities =
354 		DMX_TS_FILTERING | DMX_SECTION_FILTERING |
355 		DMX_MEMORY_BASED_FILTERING;
356 	dvb->demux.priv       = port;
357 	dvb->demux.filternum  = 256;
358 	dvb->demux.feednum    = 256;
359 	dvb->demux.start_feed = saa7164_dvb_start_feed;
360 	dvb->demux.stop_feed  = saa7164_dvb_stop_feed;
361 	result = dvb_dmx_init(&dvb->demux);
362 	if (result < 0) {
363 		printk(KERN_ERR "%s: dvb_dmx_init failed (errno = %d)\n",
364 		       DRIVER_NAME, result);
365 		goto fail_dmx;
366 	}
367 
368 	dvb->dmxdev.filternum    = 256;
369 	dvb->dmxdev.demux        = &dvb->demux.dmx;
370 	dvb->dmxdev.capabilities = 0;
371 	result = dvb_dmxdev_init(&dvb->dmxdev, &dvb->adapter);
372 	if (result < 0) {
373 		printk(KERN_ERR "%s: dvb_dmxdev_init failed (errno = %d)\n",
374 		       DRIVER_NAME, result);
375 		goto fail_dmxdev;
376 	}
377 
378 	dvb->fe_hw.source = DMX_FRONTEND_0;
379 	result = dvb->demux.dmx.add_frontend(&dvb->demux.dmx, &dvb->fe_hw);
380 	if (result < 0) {
381 		printk(KERN_ERR "%s: add_frontend failed "
382 		       "(DMX_FRONTEND_0, errno = %d)\n", DRIVER_NAME, result);
383 		goto fail_fe_hw;
384 	}
385 
386 	dvb->fe_mem.source = DMX_MEMORY_FE;
387 	result = dvb->demux.dmx.add_frontend(&dvb->demux.dmx, &dvb->fe_mem);
388 	if (result < 0) {
389 		printk(KERN_ERR "%s: add_frontend failed "
390 		       "(DMX_MEMORY_FE, errno = %d)\n", DRIVER_NAME, result);
391 		goto fail_fe_mem;
392 	}
393 
394 	result = dvb->demux.dmx.connect_frontend(&dvb->demux.dmx, &dvb->fe_hw);
395 	if (result < 0) {
396 		printk(KERN_ERR "%s: connect_frontend failed (errno = %d)\n",
397 		       DRIVER_NAME, result);
398 		goto fail_fe_conn;
399 	}
400 
401 	/* register network adapter */
402 	dvb_net_init(&dvb->adapter, &dvb->net, &dvb->demux.dmx);
403 	return 0;
404 
405 fail_fe_conn:
406 	dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_mem);
407 fail_fe_mem:
408 	dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_hw);
409 fail_fe_hw:
410 	dvb_dmxdev_release(&dvb->dmxdev);
411 fail_dmxdev:
412 	dvb_dmx_release(&dvb->demux);
413 fail_dmx:
414 	dvb_unregister_frontend(dvb->frontend);
415 fail_frontend:
416 	dvb_frontend_detach(dvb->frontend);
417 	dvb_unregister_adapter(&dvb->adapter);
418 fail_adapter:
419 	return result;
420 }
421 
422 int saa7164_dvb_unregister(struct saa7164_port *port)
423 {
424 	struct saa7164_dvb *dvb = &port->dvb;
425 	struct saa7164_dev *dev = port->dev;
426 	struct saa7164_buffer *b;
427 	struct list_head *c, *n;
428 
429 	dprintk(DBGLVL_DVB, "%s()\n", __func__);
430 
431 	if (port->type != SAA7164_MPEG_DVB)
432 		BUG();
433 
434 	/* Remove any allocated buffers */
435 	mutex_lock(&port->dmaqueue_lock);
436 	list_for_each_safe(c, n, &port->dmaqueue.list) {
437 		b = list_entry(c, struct saa7164_buffer, list);
438 		list_del(c);
439 		saa7164_buffer_dealloc(b);
440 	}
441 	mutex_unlock(&port->dmaqueue_lock);
442 
443 	if (dvb->frontend == NULL)
444 		return 0;
445 
446 	dvb_net_release(&dvb->net);
447 	dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_mem);
448 	dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_hw);
449 	dvb_dmxdev_release(&dvb->dmxdev);
450 	dvb_dmx_release(&dvb->demux);
451 	dvb_unregister_frontend(dvb->frontend);
452 	dvb_frontend_detach(dvb->frontend);
453 	dvb_unregister_adapter(&dvb->adapter);
454 	return 0;
455 }
456 
457 /* All the DVB attach calls go here, this function get's modified
458  * for each new card.
459  */
460 int saa7164_dvb_register(struct saa7164_port *port)
461 {
462 	struct saa7164_dev *dev = port->dev;
463 	struct saa7164_dvb *dvb = &port->dvb;
464 	struct saa7164_i2c *i2c_bus = NULL;
465 	int ret;
466 
467 	dprintk(DBGLVL_DVB, "%s()\n", __func__);
468 
469 	/* init frontend */
470 	switch (dev->board) {
471 	case SAA7164_BOARD_HAUPPAUGE_HVR2200:
472 	case SAA7164_BOARD_HAUPPAUGE_HVR2200_2:
473 	case SAA7164_BOARD_HAUPPAUGE_HVR2200_3:
474 	case SAA7164_BOARD_HAUPPAUGE_HVR2200_4:
475 	case SAA7164_BOARD_HAUPPAUGE_HVR2200_5:
476 		i2c_bus = &dev->i2c_bus[port->nr + 1];
477 		switch (port->nr) {
478 		case 0:
479 			port->dvb.frontend = dvb_attach(tda10048_attach,
480 				&hauppauge_hvr2200_1_config,
481 				&i2c_bus->i2c_adap);
482 
483 			if (port->dvb.frontend != NULL) {
484 				/* TODO: addr is in the card struct */
485 				dvb_attach(tda18271_attach, port->dvb.frontend,
486 					0xc0 >> 1, &i2c_bus->i2c_adap,
487 					&hauppauge_hvr22x0_tuner_config);
488 			}
489 
490 			break;
491 		case 1:
492 			port->dvb.frontend = dvb_attach(tda10048_attach,
493 				&hauppauge_hvr2200_2_config,
494 				&i2c_bus->i2c_adap);
495 
496 			if (port->dvb.frontend != NULL) {
497 				/* TODO: addr is in the card struct */
498 				dvb_attach(tda18271_attach, port->dvb.frontend,
499 					0xc0 >> 1, &i2c_bus->i2c_adap,
500 					&hauppauge_hvr22x0s_tuner_config);
501 			}
502 
503 			break;
504 		}
505 		break;
506 	case SAA7164_BOARD_HAUPPAUGE_HVR2250:
507 	case SAA7164_BOARD_HAUPPAUGE_HVR2250_2:
508 	case SAA7164_BOARD_HAUPPAUGE_HVR2250_3:
509 		i2c_bus = &dev->i2c_bus[port->nr + 1];
510 
511 		port->dvb.frontend = dvb_attach(s5h1411_attach,
512 			&hauppauge_s5h1411_config,
513 			&i2c_bus->i2c_adap);
514 
515 		if (port->dvb.frontend != NULL) {
516 			if (port->nr == 0) {
517 				/* Master TDA18271 */
518 				/* TODO: addr is in the card struct */
519 				dvb_attach(tda18271_attach, port->dvb.frontend,
520 					0xc0 >> 1, &i2c_bus->i2c_adap,
521 					&hauppauge_hvr22x0_tuner_config);
522 			} else {
523 				/* Slave TDA18271 */
524 				dvb_attach(tda18271_attach, port->dvb.frontend,
525 					0xc0 >> 1, &i2c_bus->i2c_adap,
526 					&hauppauge_hvr22x0s_tuner_config);
527 			}
528 		}
529 
530 		break;
531 	default:
532 		printk(KERN_ERR "%s: The frontend isn't supported\n",
533 		       dev->name);
534 		break;
535 	}
536 	if (NULL == dvb->frontend) {
537 		printk(KERN_ERR "%s() Frontend initialization failed\n",
538 		       __func__);
539 		return -1;
540 	}
541 
542 	/* register everything */
543 	ret = dvb_register(port);
544 	if (ret < 0) {
545 		if (dvb->frontend->ops.release)
546 			dvb->frontend->ops.release(dvb->frontend);
547 		return ret;
548 	}
549 
550 	return 0;
551 }
552 
553