dm1105.c 29.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*
 * dm1105.c - driver for DVB cards based on SDMC DM1105 PCI chip
 *
 * Copyright (C) 2008 Igor M. Liplianin <liplianin@me.by>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */

#include <linux/i2c.h>
23
#include <linux/i2c-algo-bit.h>
24
#include <linux/init.h>
25
#include <linux/interrupt.h>
26 27 28 29 30
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/proc_fs.h>
#include <linux/pci.h>
#include <linux/dma-mapping.h>
31
#include <linux/slab.h>
32
#include <media/rc-core.h>
33 34 35 36 37 38 39 40 41 42

#include "demux.h"
#include "dmxdev.h"
#include "dvb_demux.h"
#include "dvb_frontend.h"
#include "dvb_net.h"
#include "dvbdev.h"
#include "dvb-pll.h"

#include "stv0299.h"
43 44
#include "stv0288.h"
#include "stb6000.h"
45
#include "si21xx.h"
46
#include "cx24116.h"
47
#include "z0194a.h"
48
#include "ts2020.h"
49
#include "ds3000.h"
50

51 52
#define MODULE_NAME "dm1105"

53 54
#define UNSET (-1U)

55 56 57 58 59 60
#define DM1105_BOARD_NOAUTO			UNSET
#define DM1105_BOARD_UNKNOWN			0
#define DM1105_BOARD_DVBWORLD_2002		1
#define DM1105_BOARD_DVBWORLD_2004		2
#define DM1105_BOARD_AXESS_DM05			3
#define DM1105_BOARD_UNBRANDED_I2C_ON_GPIO	4
61

62 63 64 65 66 67 68
/* ----------------------------------------------- */
/*
 * PCI ID's
 */
#ifndef PCI_VENDOR_ID_TRIGEM
#define PCI_VENDOR_ID_TRIGEM	0x109f
#endif
69 70 71
#ifndef PCI_VENDOR_ID_AXESS
#define PCI_VENDOR_ID_AXESS	0x195d
#endif
72 73 74 75 76 77 78 79 80
#ifndef PCI_DEVICE_ID_DM1105
#define PCI_DEVICE_ID_DM1105	0x036f
#endif
#ifndef PCI_DEVICE_ID_DW2002
#define PCI_DEVICE_ID_DW2002	0x2002
#endif
#ifndef PCI_DEVICE_ID_DW2004
#define PCI_DEVICE_ID_DW2004	0x2004
#endif
81 82 83
#ifndef PCI_DEVICE_ID_DM05
#define PCI_DEVICE_ID_DM05	0x1105
#endif
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
/* ----------------------------------------------- */
/* sdmc dm1105 registers */

/* TS Control */
#define DM1105_TSCTR				0x00
#define DM1105_DTALENTH				0x04

/* GPIO Interface */
#define DM1105_GPIOVAL				0x08
#define DM1105_GPIOCTR				0x0c

/* PID serial number */
#define DM1105_PIDN				0x10

/* Odd-even secret key select */
#define DM1105_CWSEL				0x14

/* Host Command Interface */
#define DM1105_HOST_CTR				0x18
#define DM1105_HOST_AD				0x1c

/* PCI Interface */
#define DM1105_CR				0x30
#define DM1105_RST				0x34
#define DM1105_STADR				0x38
#define DM1105_RLEN				0x3c
#define DM1105_WRP				0x40
#define DM1105_INTCNT				0x44
#define DM1105_INTMAK				0x48
#define DM1105_INTSTS				0x4c

/* CW Value */
#define DM1105_ODD				0x50
#define DM1105_EVEN				0x58

/* PID Value */
#define DM1105_PID				0x60

/* IR Control */
#define DM1105_IRCTR				0x64
#define DM1105_IRMODE				0x68
#define DM1105_SYSTEMCODE			0x6c
#define DM1105_IRCODE				0x70

/* Unknown Values */
#define DM1105_ENCRYPT				0x74
#define DM1105_VER				0x7c

/* I2C Interface */
#define DM1105_I2CCTR				0x80
#define DM1105_I2CSTS				0x81
#define DM1105_I2CDAT				0x82
#define DM1105_I2C_RA				0x83
/* ----------------------------------------------- */
/* Interrupt Mask Bits */

#define INTMAK_TSIRQM				0x01
#define INTMAK_HIRQM				0x04
#define INTMAK_IRM				0x08
#define INTMAK_ALLMASK				(INTMAK_TSIRQM | \
						INTMAK_HIRQM | \
						INTMAK_IRM)
#define INTMAK_NONEMASK				0x00

/* Interrupt Status Bits */
#define INTSTS_TSIRQ				0x01
#define INTSTS_HIRQ				0x04
#define INTSTS_IR				0x08

/* IR Control Bits */
#define DM1105_IR_EN				0x01
#define DM1105_SYS_CHK				0x02
#define DM1105_REP_FLG				0x08

/* EEPROM addr */
#define IIC_24C01_addr				0xa0
/* Max board count */
#define DM1105_MAX				0x04

#define DRIVER_NAME				"dm1105"
164
#define DM1105_I2C_GPIO_NAME			"dm1105-gpio"
165 166 167 168 169

#define DM1105_DMA_PACKETS			47
#define DM1105_DMA_PACKET_LENGTH		(128*4)
#define DM1105_DMA_BYTES			(128 * 4 * DM1105_DMA_PACKETS)

170 171 172 173 174 175 176 177 178
/*  */
#define GPIO08					(1 << 8)
#define GPIO13					(1 << 13)
#define GPIO14					(1 << 14)
#define GPIO15					(1 << 15)
#define GPIO16					(1 << 16)
#define GPIO17					(1 << 17)
#define GPIO_ALL				0x03ffff

179
/* GPIO's for LNB power control */
180 181 182 183
#define DM1105_LNB_MASK				(GPIO_ALL & ~(GPIO14 | GPIO13))
#define DM1105_LNB_OFF				GPIO17
#define DM1105_LNB_13V				(GPIO16 | GPIO08)
#define DM1105_LNB_18V				GPIO08
184

185
/* GPIO's for LNB power control for Axess DM05 */
186 187 188 189 190 191 192 193 194 195
#define DM05_LNB_MASK				(GPIO_ALL & ~(GPIO14 | GPIO13))
#define DM05_LNB_OFF				GPIO17/* actually 13v */
#define DM05_LNB_13V				GPIO17
#define DM05_LNB_18V				(GPIO17 | GPIO16)

/* GPIO's for LNB power control for unbranded with I2C on GPIO */
#define UNBR_LNB_MASK				(GPIO17 | GPIO16)
#define UNBR_LNB_OFF				0
#define UNBR_LNB_13V				GPIO17
#define UNBR_LNB_18V				(GPIO17 | GPIO16)
196

197 198 199 200
static unsigned int card[]  = {[0 ... 3] = UNSET };
module_param_array(card,  int, NULL, 0444);
MODULE_PARM_DESC(card, "card type");

201 202 203 204
static int ir_debug;
module_param(ir_debug, int, 0644);
MODULE_PARM_DESC(ir_debug, "enable debugging information for IR decoding");

205 206
static unsigned int dm1105_devcount;

207 208
DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);

209
struct dm1105_board {
210 211 212 213 214
	char	*name;
	struct	{
		u32	mask, off, v13, v18;
	} lnb;
	u32	gpio_scl, gpio_sda;
215 216 217 218 219 220 221 222 223 224 225
};

struct dm1105_subid {
	u16     subvendor;
	u16     subdevice;
	u32     card;
};

static const struct dm1105_board dm1105_boards[] = {
	[DM1105_BOARD_UNKNOWN] = {
		.name		= "UNKNOWN/GENERIC",
226 227 228 229 230 231
		.lnb = {
			.mask = DM1105_LNB_MASK,
			.off = DM1105_LNB_OFF,
			.v13 = DM1105_LNB_13V,
			.v18 = DM1105_LNB_18V,
		},
232 233 234
	},
	[DM1105_BOARD_DVBWORLD_2002] = {
		.name		= "DVBWorld PCI 2002",
235 236 237 238 239 240
		.lnb = {
			.mask = DM1105_LNB_MASK,
			.off = DM1105_LNB_OFF,
			.v13 = DM1105_LNB_13V,
			.v18 = DM1105_LNB_18V,
		},
241 242 243
	},
	[DM1105_BOARD_DVBWORLD_2004] = {
		.name		= "DVBWorld PCI 2004",
244 245 246 247 248 249
		.lnb = {
			.mask = DM1105_LNB_MASK,
			.off = DM1105_LNB_OFF,
			.v13 = DM1105_LNB_13V,
			.v18 = DM1105_LNB_18V,
		},
250 251 252
	},
	[DM1105_BOARD_AXESS_DM05] = {
		.name		= "Axess/EasyTv DM05",
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
		.lnb = {
			.mask = DM05_LNB_MASK,
			.off = DM05_LNB_OFF,
			.v13 = DM05_LNB_13V,
			.v18 = DM05_LNB_18V,
		},
	},
	[DM1105_BOARD_UNBRANDED_I2C_ON_GPIO] = {
		.name		= "Unbranded DM1105 with i2c on GPIOs",
		.lnb = {
			.mask = UNBR_LNB_MASK,
			.off = UNBR_LNB_OFF,
			.v13 = UNBR_LNB_13V,
			.v18 = UNBR_LNB_18V,
		},
		.gpio_scl	= GPIO14,
		.gpio_sda	= GPIO13,
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
	},
};

static const struct dm1105_subid dm1105_subids[] = {
	{
		.subvendor = 0x0000,
		.subdevice = 0x2002,
		.card      = DM1105_BOARD_DVBWORLD_2002,
	}, {
		.subvendor = 0x0001,
		.subdevice = 0x2002,
		.card      = DM1105_BOARD_DVBWORLD_2002,
	}, {
		.subvendor = 0x0000,
		.subdevice = 0x2004,
		.card      = DM1105_BOARD_DVBWORLD_2004,
	}, {
		.subvendor = 0x0001,
		.subdevice = 0x2004,
		.card      = DM1105_BOARD_DVBWORLD_2004,
	}, {
		.subvendor = 0x195d,
		.subdevice = 0x1105,
		.card      = DM1105_BOARD_AXESS_DM05,
	},
};

static void dm1105_card_list(struct pci_dev *pci)
{
	int i;

	if (0 == pci->subsystem_vendor &&
			0 == pci->subsystem_device) {
		printk(KERN_ERR
			"dm1105: Your board has no valid PCI Subsystem ID\n"
			"dm1105: and thus can't be autodetected\n"
			"dm1105: Please pass card=<n> insmod option to\n"
			"dm1105: workaround that.  Redirect complaints to\n"
			"dm1105: the vendor of the TV card.  Best regards,\n"
			"dm1105: -- tux\n");
	} else {
		printk(KERN_ERR
			"dm1105: Your board isn't known (yet) to the driver.\n"
			"dm1105: You can try to pick one of the existing\n"
			"dm1105: card configs via card=<n> insmod option.\n"
			"dm1105: Updating to the latest version might help\n"
			"dm1105: as well.\n");
	}
	printk(KERN_ERR "Here is a list of valid choices for the card=<n> "
		   "insmod option:\n");
	for (i = 0; i < ARRAY_SIZE(dm1105_boards); i++)
		printk(KERN_ERR "dm1105:    card=%d -> %s\n",
				i, dm1105_boards[i].name);
}

325 326
/* infrared remote control */
struct infrared {
327
	struct rc_dev		*dev;
328
	char			input_phys[32];
329
	struct work_struct	work;
330 331 332
	u32			ir_command;
};

333
struct dm1105_dev {
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
	/* pci */
	struct pci_dev *pdev;
	u8 __iomem *io_mem;

	/* ir */
	struct infrared ir;

	/* dvb */
	struct dmx_frontend hw_frontend;
	struct dmx_frontend mem_frontend;
	struct dmxdev dmxdev;
	struct dvb_adapter dvb_adapter;
	struct dvb_demux demux;
	struct dvb_frontend *fe;
	struct dvb_net dvbnet;
	unsigned int full_ts_users;
350 351
	unsigned int boardnr;
	int nr;
352 353 354

	/* i2c */
	struct i2c_adapter i2c_adap;
355 356
	struct i2c_adapter i2c_bb_adap;
	struct i2c_algo_bit_data i2c_bit;
357

358 359
	/* irq */
	struct work_struct work;
360 361
	struct workqueue_struct *wq;
	char wqn[16];
362

363 364 365 366
	/* dma */
	dma_addr_t dma_addr;
	unsigned char *ts_buf;
	u32 wrp;
367
	u32 nextwrp;
368 369 370 371 372 373
	u32 buffer_size;
	unsigned int	PacketErrorCount;
	unsigned int dmarst;
	spinlock_t lock;
};

374
#define dm_io_mem(reg)	((unsigned long)(&dev->io_mem[reg]))
375

376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391
#define dm_readb(reg)		inb(dm_io_mem(reg))
#define dm_writeb(reg, value)	outb((value), (dm_io_mem(reg)))

#define dm_readw(reg)		inw(dm_io_mem(reg))
#define dm_writew(reg, value)	outw((value), (dm_io_mem(reg)))

#define dm_readl(reg)		inl(dm_io_mem(reg))
#define dm_writel(reg, value)	outl((value), (dm_io_mem(reg)))

#define dm_andorl(reg, mask, value) \
	outl((inl(dm_io_mem(reg)) & ~(mask)) |\
		((value) & (mask)), (dm_io_mem(reg)))

#define dm_setl(reg, bit)	dm_andorl((reg), (bit), (bit))
#define dm_clearl(reg, bit)	dm_andorl((reg), (bit), 0)

392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488
/* The chip has 18 GPIOs. In HOST mode GPIO's used as 15 bit address lines,
 so we can use only 3 GPIO's from GPIO15 to GPIO17.
 Here I don't check whether HOST is enebled as it is not implemented yet.
 */
static void dm1105_gpio_set(struct dm1105_dev *dev, u32 mask)
{
	if (mask & 0xfffc0000)
		printk(KERN_ERR "%s: Only 18 GPIO's are allowed\n", __func__);

	if (mask & 0x0003ffff)
		dm_setl(DM1105_GPIOVAL, mask & 0x0003ffff);

}

static void dm1105_gpio_clear(struct dm1105_dev *dev, u32 mask)
{
	if (mask & 0xfffc0000)
		printk(KERN_ERR "%s: Only 18 GPIO's are allowed\n", __func__);

	if (mask & 0x0003ffff)
		dm_clearl(DM1105_GPIOVAL, mask & 0x0003ffff);

}

static void dm1105_gpio_andor(struct dm1105_dev *dev, u32 mask, u32 val)
{
	if (mask & 0xfffc0000)
		printk(KERN_ERR "%s: Only 18 GPIO's are allowed\n", __func__);

	if (mask & 0x0003ffff)
		dm_andorl(DM1105_GPIOVAL, mask & 0x0003ffff, val);

}

static u32 dm1105_gpio_get(struct dm1105_dev *dev, u32 mask)
{
	if (mask & 0xfffc0000)
		printk(KERN_ERR "%s: Only 18 GPIO's are allowed\n", __func__);

	if (mask & 0x0003ffff)
		return dm_readl(DM1105_GPIOVAL) & mask & 0x0003ffff;

	return 0;
}

static void dm1105_gpio_enable(struct dm1105_dev *dev, u32 mask, int asoutput)
{
	if (mask & 0xfffc0000)
		printk(KERN_ERR "%s: Only 18 GPIO's are allowed\n", __func__);

	if ((mask & 0x0003ffff) && asoutput)
		dm_clearl(DM1105_GPIOCTR, mask & 0x0003ffff);
	else if ((mask & 0x0003ffff) && !asoutput)
		dm_setl(DM1105_GPIOCTR, mask & 0x0003ffff);

}

static void dm1105_setline(struct dm1105_dev *dev, u32 line, int state)
{
	if (state)
		dm1105_gpio_enable(dev, line, 0);
	else {
		dm1105_gpio_enable(dev, line, 1);
		dm1105_gpio_clear(dev, line);
	}
}

static void dm1105_setsda(void *data, int state)
{
	struct dm1105_dev *dev = data;

	dm1105_setline(dev, dm1105_boards[dev->boardnr].gpio_sda, state);
}

static void dm1105_setscl(void *data, int state)
{
	struct dm1105_dev *dev = data;

	dm1105_setline(dev, dm1105_boards[dev->boardnr].gpio_scl, state);
}

static int dm1105_getsda(void *data)
{
	struct dm1105_dev *dev = data;

	return dm1105_gpio_get(dev, dm1105_boards[dev->boardnr].gpio_sda)
									? 1 : 0;
}

static int dm1105_getscl(void *data)
{
	struct dm1105_dev *dev = data;

	return dm1105_gpio_get(dev, dm1105_boards[dev->boardnr].gpio_scl)
									? 1 : 0;
}

489 490 491
static int dm1105_i2c_xfer(struct i2c_adapter *i2c_adap,
			    struct i2c_msg *msgs, int num)
{
492
	struct dm1105_dev *dev ;
493 494 495 496

	int addr, rc, i, j, k, len, byte, data;
	u8 status;

497
	dev = i2c_adap->algo_data;
498
	for (i = 0; i < num; i++) {
499
		dm_writeb(DM1105_I2CCTR, 0x00);
500 501 502 503
		if (msgs[i].flags & I2C_M_RD) {
			/* read bytes */
			addr  = msgs[i].addr << 1;
			addr |= 1;
504
			dm_writeb(DM1105_I2CDAT, addr);
505
			for (byte = 0; byte < msgs[i].len; byte++)
506
				dm_writeb(DM1105_I2CDAT + byte + 1, 0);
507

508
			dm_writeb(DM1105_I2CCTR, 0x81 + msgs[i].len);
509 510
			for (j = 0; j < 55; j++) {
				mdelay(10);
511
				status = dm_readb(DM1105_I2CSTS);
512 513 514 515 516 517 518
				if ((status & 0xc0) == 0x40)
					break;
			}
			if (j >= 55)
				return -1;

			for (byte = 0; byte < msgs[i].len; byte++) {
519
				rc = dm_readb(DM1105_I2CDAT + byte + 1);
520 521 522 523
				if (rc < 0)
					goto err;
				msgs[i].buf[byte] = rc;
			}
524 525 526 527 528 529
		} else if ((msgs[i].buf[0] == 0xf7) && (msgs[i].addr == 0x55)) {
			/* prepaired for cx24116 firmware */
			/* Write in small blocks */
			len = msgs[i].len - 1;
			k = 1;
			do {
530 531
				dm_writeb(DM1105_I2CDAT, msgs[i].addr << 1);
				dm_writeb(DM1105_I2CDAT + 1, 0xf7);
532 533
				for (byte = 0; byte < (len > 48 ? 48 : len); byte++) {
					data = msgs[i].buf[k + byte];
534
					dm_writeb(DM1105_I2CDAT + byte + 2, data);
535
				}
536
				dm_writeb(DM1105_I2CCTR, 0x82 + (len > 48 ? 48 : len));
537 538
				for (j = 0; j < 25; j++) {
					mdelay(10);
539
					status = dm_readb(DM1105_I2CSTS);
540 541 542 543 544 545
					if ((status & 0xc0) == 0x40)
						break;
				}

				if (j >= 25)
					return -1;
546 547 548 549 550 551

				k += 48;
				len -= 48;
			} while (len > 0);
		} else {
			/* write bytes */
552
			dm_writeb(DM1105_I2CDAT, msgs[i].addr << 1);
553 554
			for (byte = 0; byte < msgs[i].len; byte++) {
				data = msgs[i].buf[byte];
555
				dm_writeb(DM1105_I2CDAT + byte + 1, data);
556
			}
557
			dm_writeb(DM1105_I2CCTR, 0x81 + msgs[i].len);
558 559
			for (j = 0; j < 25; j++) {
				mdelay(10);
560
				status = dm_readb(DM1105_I2CSTS);
561 562
				if ((status & 0xc0) == 0x40)
					break;
563
			}
564 565 566

			if (j >= 25)
				return -1;
567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583
		}
	}
	return num;
 err:
	return rc;
}

static u32 functionality(struct i2c_adapter *adap)
{
	return I2C_FUNC_I2C;
}

static struct i2c_algorithm dm1105_algo = {
	.master_xfer   = dm1105_i2c_xfer,
	.functionality = functionality,
};

584
static inline struct dm1105_dev *feed_to_dm1105_dev(struct dvb_demux_feed *feed)
585
{
586
	return container_of(feed->demux, struct dm1105_dev, demux);
587 588
}

589
static inline struct dm1105_dev *frontend_to_dm1105_dev(struct dvb_frontend *fe)
590
{
591
	return container_of(fe->dvb, struct dm1105_dev, dvb_adapter);
592 593
}

594
static int dm1105_set_voltage(struct dvb_frontend *fe, fe_sec_voltage_t voltage)
595
{
596
	struct dm1105_dev *dev = frontend_to_dm1105_dev(fe);
597

598
	dm1105_gpio_enable(dev, dm1105_boards[dev->boardnr].lnb.mask, 1);
599
	if (voltage == SEC_VOLTAGE_18)
600 601 602
		dm1105_gpio_andor(dev,
				dm1105_boards[dev->boardnr].lnb.mask,
				dm1105_boards[dev->boardnr].lnb.v18);
603
	else if (voltage == SEC_VOLTAGE_13)
604 605 606
		dm1105_gpio_andor(dev,
				dm1105_boards[dev->boardnr].lnb.mask,
				dm1105_boards[dev->boardnr].lnb.v13);
607
	else
608 609 610
		dm1105_gpio_andor(dev,
				dm1105_boards[dev->boardnr].lnb.mask,
				dm1105_boards[dev->boardnr].lnb.off);
611 612 613 614

	return 0;
}

615
static void dm1105_set_dma_addr(struct dm1105_dev *dev)
616
{
617
	dm_writel(DM1105_STADR, (__force u32)cpu_to_le32(dev->dma_addr));
618 619
}

620
static int dm1105_dma_map(struct dm1105_dev *dev)
621
{
622 623 624
	dev->ts_buf = pci_alloc_consistent(dev->pdev,
					6 * DM1105_DMA_BYTES,
					&dev->dma_addr);
625

626
	return !dev->ts_buf;
627 628
}

629
static void dm1105_dma_unmap(struct dm1105_dev *dev)
630
{
631 632 633 634
	pci_free_consistent(dev->pdev,
			6 * DM1105_DMA_BYTES,
			dev->ts_buf,
			dev->dma_addr);
635 636
}

637
static void dm1105_enable_irqs(struct dm1105_dev *dev)
638
{
639 640
	dm_writeb(DM1105_INTMAK, INTMAK_ALLMASK);
	dm_writeb(DM1105_CR, 1);
641 642
}

643
static void dm1105_disable_irqs(struct dm1105_dev *dev)
644
{
645 646
	dm_writeb(DM1105_INTMAK, INTMAK_IRM);
	dm_writeb(DM1105_CR, 0);
647 648
}

649
static int dm1105_start_feed(struct dvb_demux_feed *f)
650
{
651
	struct dm1105_dev *dev = feed_to_dm1105_dev(f);
652

653 654
	if (dev->full_ts_users++ == 0)
		dm1105_enable_irqs(dev);
655 656 657 658

	return 0;
}

659
static int dm1105_stop_feed(struct dvb_demux_feed *f)
660
{
661
	struct dm1105_dev *dev = feed_to_dm1105_dev(f);
662

663 664
	if (--dev->full_ts_users == 0)
		dm1105_disable_irqs(dev);
665 666 667 668

	return 0;
}

669 670
/* ir work handler */
static void dm1105_emit_key(struct work_struct *work)
671
{
672
	struct infrared *ir = container_of(work, struct infrared, work);
673 674 675
	u32 ircom = ir->ir_command;
	u8 data;

676 677 678
	if (ir_debug)
		printk(KERN_INFO "%s: received byte 0x%04x\n", __func__, ircom);

679 680
	data = (ircom >> 8) & 0x7f;

681 682
	/* FIXME: UNKNOWN because we don't generate a full NEC scancode (yet?) */
	rc_keydown(ir->dev, RC_TYPE_UNKNOWN, data, 0);
683 684
}

685 686 687
/* work handler */
static void dm1105_dmx_buffer(struct work_struct *work)
{
688
	struct dm1105_dev *dev = container_of(work, struct dm1105_dev, work);
689
	unsigned int nbpackets;
690 691
	u32 oldwrp = dev->wrp;
	u32 nextwrp = dev->nextwrp;
692

693 694 695 696
	if (!((dev->ts_buf[oldwrp] == 0x47) &&
			(dev->ts_buf[oldwrp + 188] == 0x47) &&
			(dev->ts_buf[oldwrp + 188 * 2] == 0x47))) {
		dev->PacketErrorCount++;
697
		/* bad packet found */
698 699
		if ((dev->PacketErrorCount >= 2) &&
				(dev->dmarst == 0)) {
700
			dm_writeb(DM1105_RST, 1);
701 702 703
			dev->wrp = 0;
			dev->PacketErrorCount = 0;
			dev->dmarst = 0;
704 705 706 707 708
			return;
		}
	}

	if (nextwrp < oldwrp) {
709 710
		memcpy(dev->ts_buf + dev->buffer_size, dev->ts_buf, nextwrp);
		nbpackets = ((dev->buffer_size - oldwrp) + nextwrp) / 188;
711 712 713
	} else
		nbpackets = (nextwrp - oldwrp) / 188;

714 715
	dev->wrp = nextwrp;
	dvb_dmx_swfilter_packets(&dev->demux, &dev->ts_buf[oldwrp], nbpackets);
716 717
}

718
static irqreturn_t dm1105_irq(int irq, void *dev_id)
719
{
720
	struct dm1105_dev *dev = dev_id;
721 722

	/* Read-Write INSTS Ack's Interrupt for DM1105 chip 16.03.2008 */
723 724
	unsigned int intsts = dm_readb(DM1105_INTSTS);
	dm_writeb(DM1105_INTSTS, intsts);
725 726 727 728

	switch (intsts) {
	case INTSTS_TSIRQ:
	case (INTSTS_TSIRQ | INTSTS_IR):
729
		dev->nextwrp = dm_readl(DM1105_WRP) - dm_readl(DM1105_STADR);
730
		queue_work(dev->wq, &dev->work);
731 732
		break;
	case INTSTS_IR:
733
		dev->ir.ir_command = dm_readl(DM1105_IRCODE);
734
		schedule_work(&dev->ir.work);
735 736 737
		break;
	}

738
	return IRQ_HANDLED;
739 740
}

741
static int dm1105_ir_init(struct dm1105_dev *dm1105)
742
{
743
	struct rc_dev *dev;
744
	int err = -ENOMEM;
745

746 747
	dev = rc_allocate_device();
	if (!dev)
748 749 750 751 752
		return -ENOMEM;

	snprintf(dm1105->ir.input_phys, sizeof(dm1105->ir.input_phys),
		"pci-%s/ir0", pci_name(dm1105->pdev));

753 754 755 756 757 758 759
	dev->driver_name = MODULE_NAME;
	dev->map_name = RC_MAP_DM1105_NEC;
	dev->driver_type = RC_DRIVER_SCANCODE;
	dev->input_name = "DVB on-card IR receiver";
	dev->input_phys = dm1105->ir.input_phys;
	dev->input_id.bustype = BUS_PCI;
	dev->input_id.version = 1;
760
	if (dm1105->pdev->subsystem_vendor) {
761 762
		dev->input_id.vendor = dm1105->pdev->subsystem_vendor;
		dev->input_id.product = dm1105->pdev->subsystem_device;
763
	} else {
764 765
		dev->input_id.vendor = dm1105->pdev->vendor;
		dev->input_id.product = dm1105->pdev->device;
766
	}
767
	dev->dev.parent = &dm1105->pdev->dev;
768 769 770

	INIT_WORK(&dm1105->ir.work, dm1105_emit_key);

771
	err = rc_register_device(dev);
772
	if (err < 0) {
773
		rc_free_device(dev);
774 775
		return err;
	}
776

777
	dm1105->ir.dev = dev;
778
	return 0;
779 780
}

781
static void dm1105_ir_exit(struct dm1105_dev *dm1105)
782
{
783
	rc_unregister_device(dm1105->ir.dev);
784 785
}

786
static int dm1105_hw_init(struct dm1105_dev *dev)
787
{
788
	dm1105_disable_irqs(dev);
789

790
	dm_writeb(DM1105_HOST_CTR, 0);
791 792

	/*DATALEN 188,*/
793
	dm_writeb(DM1105_DTALENTH, 188);
794
	/*TS_STRT TS_VALP MSBFIRST TS_MODE ALPAS TSPES*/
795
	dm_writew(DM1105_TSCTR, 0xc10a);
796 797

	/* map DMA and set address */
798 799
	dm1105_dma_map(dev);
	dm1105_set_dma_addr(dev);
800
	/* big buffer */
801 802
	dm_writel(DM1105_RLEN, 5 * DM1105_DMA_BYTES);
	dm_writeb(DM1105_INTCNT, 47);
803 804

	/* IR NEC mode enable */
805 806 807
	dm_writeb(DM1105_IRCTR, (DM1105_IR_EN | DM1105_SYS_CHK));
	dm_writeb(DM1105_IRMODE, 0);
	dm_writew(DM1105_SYSTEMCODE, 0);
808 809 810 811

	return 0;
}

812
static void dm1105_hw_exit(struct dm1105_dev *dev)
813
{
814
	dm1105_disable_irqs(dev);
815 816

	/* IR disable */
817 818
	dm_writeb(DM1105_IRCTR, 0);
	dm_writeb(DM1105_INTMAK, INTMAK_NONEMASK);
819

820
	dm1105_dma_unmap(dev);
821
}
822

823 824 825 826 827 828 829 830 831 832 833 834
static struct stv0299_config sharp_z0194a_config = {
	.demod_address = 0x68,
	.inittab = sharp_z0194a_inittab,
	.mclk = 88000000UL,
	.invert = 1,
	.skip_reinit = 0,
	.lock_output = STV0299_LOCKOUTPUT_1,
	.volt13_op0_op1 = STV0299_VOLT13_OP1,
	.min_delay_ms = 100,
	.set_symbol_rate = sharp_z0194a_set_symbol_rate,
};

835 836 837 838 839 840 841 842 843 844 845 846 847 848 849
static struct stv0288_config earda_config = {
	.demod_address = 0x68,
	.min_delay_ms = 100,
};

static struct si21xx_config serit_config = {
	.demod_address = 0x68,
	.min_delay_ms = 100,

};

static struct cx24116_config serit_sp2633_config = {
	.demod_address = 0x55,
};

850 851 852 853
static struct ds3000_config dvbworld_ds3000_config = {
	.demod_address = 0x68,
};

854 855
static struct ts2020_config dvbworld_ts2020_config  = {
	.tuner_address = 0x60,
856
	.clk_out_div = 1,
857 858
};

859
static int frontend_init(struct dm1105_dev *dev)
860 861 862
{
	int ret;

863
	switch (dev->boardnr) {
864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895
	case DM1105_BOARD_UNBRANDED_I2C_ON_GPIO:
		dm1105_gpio_enable(dev, GPIO15, 1);
		dm1105_gpio_clear(dev, GPIO15);
		msleep(100);
		dm1105_gpio_set(dev, GPIO15);
		msleep(200);
		dev->fe = dvb_attach(
			stv0299_attach, &sharp_z0194a_config,
			&dev->i2c_bb_adap);
		if (dev->fe) {
			dev->fe->ops.set_voltage = dm1105_set_voltage;
			dvb_attach(dvb_pll_attach, dev->fe, 0x60,
					&dev->i2c_bb_adap, DVB_PLL_OPERA1);
			break;
		}

		dev->fe = dvb_attach(
			stv0288_attach, &earda_config,
			&dev->i2c_bb_adap);
		if (dev->fe) {
			dev->fe->ops.set_voltage = dm1105_set_voltage;
			dvb_attach(stb6000_attach, dev->fe, 0x61,
					&dev->i2c_bb_adap);
			break;
		}

		dev->fe = dvb_attach(
			si21xx_attach, &serit_config,
			&dev->i2c_bb_adap);
		if (dev->fe)
			dev->fe->ops.set_voltage = dm1105_set_voltage;
		break;
896
	case DM1105_BOARD_DVBWORLD_2004:
897
		dev->fe = dvb_attach(
898
			cx24116_attach, &serit_sp2633_config,
899 900 901
			&dev->i2c_adap);
		if (dev->fe) {
			dev->fe->ops.set_voltage = dm1105_set_voltage;
902 903 904
			break;
		}

905
		dev->fe = dvb_attach(
906
			ds3000_attach, &dvbworld_ds3000_config,
907
			&dev->i2c_adap);
908 909 910
		if (dev->fe) {
			dvb_attach(ts2020_attach, dev->fe,
				&dvbworld_ts2020_config, &dev->i2c_adap);
911
			dev->fe->ops.set_voltage = dm1105_set_voltage;
912
		}
913

914
		break;
915 916
	case DM1105_BOARD_DVBWORLD_2002:
	case DM1105_BOARD_AXESS_DM05:
917
	default:
918
		dev->fe = dvb_attach(
919
			stv0299_attach, &sharp_z0194a_config,
920 921 922 923 924
			&dev->i2c_adap);
		if (dev->fe) {
			dev->fe->ops.set_voltage = dm1105_set_voltage;
			dvb_attach(dvb_pll_attach, dev->fe, 0x60,
					&dev->i2c_adap, DVB_PLL_OPERA1);
925
			break;
926
		}
927

928
		dev->fe = dvb_attach(
929
			stv0288_attach, &earda_config,
930 931 932 933 934
			&dev->i2c_adap);
		if (dev->fe) {
			dev->fe->ops.set_voltage = dm1105_set_voltage;
			dvb_attach(stb6000_attach, dev->fe, 0x61,
					&dev->i2c_adap);
935
			break;
936
		}
937

938
		dev->fe = dvb_attach(
939
			si21xx_attach, &serit_config,
940 941 942
			&dev->i2c_adap);
		if (dev->fe)
			dev->fe->ops.set_voltage = dm1105_set_voltage;
943

944 945
	}

946 947
	if (!dev->fe) {
		dev_err(&dev->pdev->dev, "could not attach frontend\n");
948 949 950
		return -ENODEV;
	}

951
	ret = dvb_register_frontend(&dev->dvb_adapter, dev->fe);
952
	if (ret < 0) {
953 954 955
		if (dev->fe->ops.release)
			dev->fe->ops.release(dev->fe);
		dev->fe = NULL;
956 957 958 959 960 961
		return ret;
	}

	return 0;
}

962
static void dm1105_read_mac(struct dm1105_dev *dev, u8 *mac)
963 964 965 966
{
	static u8 command[1] = { 0x28 };

	struct i2c_msg msg[] = {
967 968 969 970 971 972 973 974 975 976 977
		{
			.addr = IIC_24C01_addr >> 1,
			.flags = 0,
			.buf = command,
			.len = 1
		}, {
			.addr = IIC_24C01_addr >> 1,
			.flags = I2C_M_RD,
			.buf = mac,
			.len = 6
		},
978 979
	};

980 981
	dm1105_i2c_xfer(&dev->i2c_adap, msg , 2);
	dev_info(&dev->pdev->dev, "MAC %pM\n", mac);
982 983
}

984
static int dm1105_probe(struct pci_dev *pdev,
985 986
				  const struct pci_device_id *ent)
{
987
	struct dm1105_dev *dev;
988 989 990 991
	struct dvb_adapter *dvb_adapter;
	struct dvb_demux *dvbdemux;
	struct dmx_demux *dmx;
	int ret = -ENOMEM;
992
	int i;
993

994 995
	dev = kzalloc(sizeof(struct dm1105_dev), GFP_KERNEL);
	if (!dev)
996
		return -ENOMEM;
997

998
	/* board config */
999 1000 1001 1002 1003
	dev->nr = dm1105_devcount;
	dev->boardnr = UNSET;
	if (card[dev->nr] < ARRAY_SIZE(dm1105_boards))
		dev->boardnr = card[dev->nr];
	for (i = 0; UNSET == dev->boardnr &&
1004 1005 1006 1007 1008
				i < ARRAY_SIZE(dm1105_subids); i++)
		if (pdev->subsystem_vendor ==
			dm1105_subids[i].subvendor &&
				pdev->subsystem_device ==
					dm1105_subids[i].subdevice)
1009
			dev->boardnr = dm1105_subids[i].card;
1010

1011 1012
	if (UNSET == dev->boardnr) {
		dev->boardnr = DM1105_BOARD_UNKNOWN;
1013 1014 1015 1016
		dm1105_card_list(pdev);
	}

	dm1105_devcount++;
1017 1018 1019 1020
	dev->pdev = pdev;
	dev->buffer_size = 5 * DM1105_DMA_BYTES;
	dev->PacketErrorCount = 0;
	dev->dmarst = 0;
1021 1022 1023 1024 1025

	ret = pci_enable_device(pdev);
	if (ret < 0)
		goto err_kfree;

1026
	ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
1027 1028 1029 1030 1031 1032 1033 1034 1035
	if (ret < 0)
		goto err_pci_disable_device;

	pci_set_master(pdev);

	ret = pci_request_regions(pdev, DRIVER_NAME);
	if (ret < 0)
		goto err_pci_disable_device;

1036 1037
	dev->io_mem = pci_iomap(pdev, 0, pci_resource_len(pdev, 0));
	if (!dev->io_mem) {
1038 1039 1040 1041
		ret = -EIO;
		goto err_pci_release_regions;
	}

1042 1043
	spin_lock_init(&dev->lock);
	pci_set_drvdata(pdev, dev);
1044

1045
	ret = dm1105_hw_init(dev);
1046
	if (ret < 0)
1047
		goto err_pci_iounmap;
1048 1049

	/* i2c */
1050 1051 1052 1053 1054 1055 1056
	i2c_set_adapdata(&dev->i2c_adap, dev);
	strcpy(dev->i2c_adap.name, DRIVER_NAME);
	dev->i2c_adap.owner = THIS_MODULE;
	dev->i2c_adap.dev.parent = &pdev->dev;
	dev->i2c_adap.algo = &dm1105_algo;
	dev->i2c_adap.algo_data = dev;
	ret = i2c_add_adapter(&dev->i2c_adap);
1057 1058

	if (ret < 0)
1059
		goto err_dm1105_hw_exit;
1060

1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081
	i2c_set_adapdata(&dev->i2c_bb_adap, dev);
	strcpy(dev->i2c_bb_adap.name, DM1105_I2C_GPIO_NAME);
	dev->i2c_bb_adap.owner = THIS_MODULE;
	dev->i2c_bb_adap.dev.parent = &pdev->dev;
	dev->i2c_bb_adap.algo_data = &dev->i2c_bit;
	dev->i2c_bit.data = dev;
	dev->i2c_bit.setsda = dm1105_setsda;
	dev->i2c_bit.setscl = dm1105_setscl;
	dev->i2c_bit.getsda = dm1105_getsda;
	dev->i2c_bit.getscl = dm1105_getscl;
	dev->i2c_bit.udelay = 10;
	dev->i2c_bit.timeout = 10;

	/* Raise SCL and SDA */
	dm1105_setsda(dev, 1);
	dm1105_setscl(dev, 1);

	ret = i2c_bit_add_bus(&dev->i2c_bb_adap);
	if (ret < 0)
		goto err_i2c_del_adapter;

1082
	/* dvb */
1083
	ret = dvb_register_adapter(&dev->dvb_adapter, DRIVER_NAME,
1084 1085
					THIS_MODULE, &pdev->dev, adapter_nr);
	if (ret < 0)
1086
		goto err_i2c_del_adapters;
1087

1088
	dvb_adapter = &dev->dvb_adapter;
1089

1090
	dm1105_read_mac(dev, dvb_adapter->proposed_mac);
1091

1092
	dvbdemux = &dev->demux;
1093 1094
	dvbdemux->filternum = 256;
	dvbdemux->feednum = 256;
1095 1096
	dvbdemux->start_feed = dm1105_start_feed;
	dvbdemux->stop_feed = dm1105_stop_feed;
1097 1098 1099 1100 1101 1102 1103
	dvbdemux->dmx.capabilities = (DMX_TS_FILTERING |
			DMX_SECTION_FILTERING | DMX_MEMORY_BASED_FILTERING);
	ret = dvb_dmx_init(dvbdemux);
	if (ret < 0)
		goto err_dvb_unregister_adapter;

	dmx = &dvbdemux->dmx;
1104 1105 1106
	dev->dmxdev.filternum = 256;
	dev->dmxdev.demux = dmx;
	dev->dmxdev.capabilities = 0;
1107

1108
	ret = dvb_dmxdev_init(&dev->dmxdev, dvb_adapter);
1109 1110 1111
	if (ret < 0)
		goto err_dvb_dmx_release;

1112
	dev->hw_frontend.source = DMX_FRONTEND_0;
1113

1114
	ret = dmx->add_frontend(dmx, &dev->hw_frontend);
1115 1116 1117
	if (ret < 0)
		goto err_dvb_dmxdev_release;

1118
	dev->mem_frontend.source = DMX_MEMORY_FE;
1119

1120
	ret = dmx->add_frontend(dmx, &dev->mem_frontend);
1121 1122 1123
	if (ret < 0)
		goto err_remove_hw_frontend;

1124
	ret = dmx->connect_frontend(dmx, &dev->hw_frontend);
1125 1126 1127
	if (ret < 0)
		goto err_remove_mem_frontend;

1128 1129 1130 1131
	ret = dvb_net_init(dvb_adapter, &dev->dvbnet, dmx);
	if (ret < 0)
		goto err_disconnect_frontend;

1132
	ret = frontend_init(dev);
1133
	if (ret < 0)
1134
		goto err_dvb_net;
1135

1136
	dm1105_ir_init(dev);
1137

1138 1139 1140
	INIT_WORK(&dev->work, dm1105_dmx_buffer);
	sprintf(dev->wqn, "%s/%d", dvb_adapter->name, dvb_adapter->num);
	dev->wq = create_singlethread_workqueue(dev->wqn);
1141 1142
	if (!dev->wq) {
		ret = -ENOMEM;
1143
		goto err_dvb_net;
1144
	}
1145

1146 1147
	ret = request_irq(pdev->irq, dm1105_irq, IRQF_SHARED,
						DRIVER_NAME, dev);
1148
	if (ret < 0)
1149
		goto err_workqueue;
1150 1151

	return 0;
1152

1153
err_workqueue:
1154
	destroy_workqueue(dev->wq);
1155
err_dvb_net:
1156
	dvb_net_release(&dev->dvbnet);
1157 1158 1159
err_disconnect_frontend:
	dmx->disconnect_frontend(dmx);
err_remove_mem_frontend:
1160
	dmx->remove_frontend(dmx, &dev->mem_frontend);
1161
err_remove_hw_frontend:
1162
	dmx->remove_frontend(dmx, &dev->hw_frontend);
1163
err_dvb_dmxdev_release:
1164
	dvb_dmxdev_release(&dev->dmxdev);
1165 1166 1167 1168
err_dvb_dmx_release:
	dvb_dmx_release(dvbdemux);
err_dvb_unregister_adapter:
	dvb_unregister_adapter(dvb_adapter);
1169 1170
err_i2c_del_adapters:
	i2c_del_adapter(&dev->i2c_bb_adap);
1171
err_i2c_del_adapter:
1172 1173 1174
	i2c_del_adapter(&dev->i2c_adap);
err_dm1105_hw_exit:
	dm1105_hw_exit(dev);
1175
err_pci_iounmap:
1176
	pci_iounmap(pdev, dev->io_mem);
1177 1178 1179 1180 1181
err_pci_release_regions:
	pci_release_regions(pdev);
err_pci_disable_device:
	pci_disable_device(pdev);
err_kfree:
1182
	kfree(dev);
1183
	return ret;
1184 1185
}

1186
static void dm1105_remove(struct pci_dev *pdev)
1187
{
1188 1189 1190
	struct dm1105_dev *dev = pci_get_drvdata(pdev);
	struct dvb_adapter *dvb_adapter = &dev->dvb_adapter;
	struct dvb_demux *dvbdemux = &dev->demux;
1191 1192
	struct dmx_demux *dmx = &dvbdemux->dmx;

1193
	dm1105_ir_exit(dev);
1194
	dmx->close(dmx);
1195 1196 1197
	dvb_net_release(&dev->dvbnet);
	if (dev->fe)
		dvb_unregister_frontend(dev->fe);
1198 1199

	dmx->disconnect_frontend(dmx);
1200 1201 1202
	dmx->remove_frontend(dmx, &dev->mem_frontend);
	dmx->remove_frontend(dmx, &dev->hw_frontend);
	dvb_dmxdev_release(&dev->dmxdev);
1203 1204
	dvb_dmx_release(dvbdemux);
	dvb_unregister_adapter(dvb_adapter);
1205
	i2c_del_adapter(&dev->i2c_adap);
1206

1207
	dm1105_hw_exit(dev);
1208
	synchronize_irq(pdev->irq);
1209 1210
	free_irq(pdev->irq, dev);
	pci_iounmap(pdev, dev->io_mem);
1211 1212
	pci_release_regions(pdev);
	pci_disable_device(pdev);
1213
	dm1105_devcount--;
1214
	kfree(dev);
1215 1216
}

1217
static struct pci_device_id dm1105_id_table[] = {
1218 1219 1220 1221
	{
		.vendor = PCI_VENDOR_ID_TRIGEM,
		.device = PCI_DEVICE_ID_DM1105,
		.subvendor = PCI_ANY_ID,
1222
		.subdevice = PCI_ANY_ID,
1223 1224 1225
	}, {
		.vendor = PCI_VENDOR_ID_AXESS,
		.device = PCI_DEVICE_ID_DM05,
1226 1227
		.subvendor = PCI_ANY_ID,
		.subdevice = PCI_ANY_ID,
1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238
	}, {
		/* empty */
	},
};

MODULE_DEVICE_TABLE(pci, dm1105_id_table);

static struct pci_driver dm1105_driver = {
	.name = DRIVER_NAME,
	.id_table = dm1105_id_table,
	.probe = dm1105_probe,
1239
	.remove = dm1105_remove,
1240 1241
};

1242
module_pci_driver(dm1105_driver);
1243 1244 1245 1246

MODULE_AUTHOR("Igor M. Liplianin <liplianin@me.by>");
MODULE_DESCRIPTION("SDMC DM1105 DVB driver");
MODULE_LICENSE("GPL");