2
* cxd2099.c: Driver for the CXD2099AR Common Interface Controller
4
* Copyright (C) 2010 DigitalDevices UG
7
* This program is free software; you can redistribute it and/or
8
* modify it under the terms of the GNU General Public License
9
* version 2 only, as published by the Free Software Foundation.
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
* GNU General Public License for more details.
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., 51 Franklin Street, Fifth Floor, Boston, MA
22
* Or, point your browser to http://www.gnu.org/copyleft/gpl.html
25
#include <linux/version.h>
26
#include <linux/slab.h>
27
#include <linux/kernel.h>
28
#include <linux/module.h>
29
#include <linux/moduleparam.h>
30
#include <linux/init.h>
31
#include <linux/i2c.h>
32
#include <linux/wait.h>
33
#include <linux/delay.h>
34
#include <linux/mutex.h>
39
#define MAX_BUFFER_SIZE 248
42
struct dvb_ca_en50221 en;
44
struct i2c_adapter *i2c;
63
static int i2c_write_reg(struct i2c_adapter *adapter, u8 adr,
66
u8 m[2] = {reg, data};
67
struct i2c_msg msg = {.addr = adr, .flags = 0, .buf = m, .len = 2};
69
if (i2c_transfer(adapter, &msg, 1) != 1) {
70
printk(KERN_ERR "Failed to write to I2C register %02x@%02x!\n",
77
static int i2c_write(struct i2c_adapter *adapter, u8 adr,
80
struct i2c_msg msg = {.addr = adr, .flags = 0, .buf = data, .len = len};
82
if (i2c_transfer(adapter, &msg, 1) != 1) {
83
printk(KERN_ERR "Failed to write to I2C!\n");
89
static int i2c_read_reg(struct i2c_adapter *adapter, u8 adr,
92
struct i2c_msg msgs[2] = {{.addr = adr, .flags = 0,
93
.buf = ®, .len = 1 },
94
{.addr = adr, .flags = I2C_M_RD,
95
.buf = val, .len = 1 } };
97
if (i2c_transfer(adapter, msgs, 2) != 2) {
98
printk(KERN_ERR "error in i2c_read_reg\n");
104
static int i2c_read(struct i2c_adapter *adapter, u8 adr,
105
u8 reg, u8 *data, u8 n)
107
struct i2c_msg msgs[2] = {{.addr = adr, .flags = 0,
108
.buf = ®, .len = 1 },
109
{.addr = adr, .flags = I2C_M_RD,
110
.buf = data, .len = n } };
112
if (i2c_transfer(adapter, msgs, 2) != 2) {
113
printk(KERN_ERR "error in i2c_read\n");
119
static int read_block(struct cxd *ci, u8 adr, u8 *data, u8 n)
123
status = i2c_write_reg(ci->i2c, ci->adr, 0, adr);
125
ci->lastaddress = adr;
126
status = i2c_read(ci->i2c, ci->adr, 1, data, n);
131
static int read_reg(struct cxd *ci, u8 reg, u8 *val)
133
return read_block(ci, reg, val, 1);
137
static int read_pccard(struct cxd *ci, u16 address, u8 *data, u8 n)
140
u8 addr[3] = { 2, address&0xff, address>>8 };
142
status = i2c_write(ci->i2c, ci->adr, addr, 3);
144
status = i2c_read(ci->i2c, ci->adr, 3, data, n);
148
static int write_pccard(struct cxd *ci, u16 address, u8 *data, u8 n)
151
u8 addr[3] = { 2, address&0xff, address>>8 };
153
status = i2c_write(ci->i2c, ci->adr, addr, 3);
156
memcpy(buf+1, data, n);
157
status = i2c_write(ci->i2c, ci->adr, buf, n+1);
162
static int read_io(struct cxd *ci, u16 address, u8 *val)
165
u8 addr[3] = { 2, address&0xff, address>>8 };
167
status = i2c_write(ci->i2c, ci->adr, addr, 3);
169
status = i2c_read(ci->i2c, ci->adr, 3, val, 1);
173
static int write_io(struct cxd *ci, u16 address, u8 val)
176
u8 addr[3] = { 2, address&0xff, address>>8 };
177
u8 buf[2] = { 3, val };
179
status = i2c_write(ci->i2c, ci->adr, addr, 3);
181
status = i2c_write(ci->i2c, ci->adr, buf, 2);
187
static int write_regm(struct cxd *ci, u8 reg, u8 val, u8 mask)
191
status = i2c_write_reg(ci->i2c, ci->adr, 0, reg);
192
if (!status && reg >= 6 && reg <= 8 && mask != 0xff)
193
status = i2c_read_reg(ci->i2c, ci->adr, 1, &ci->regs[reg]);
194
ci->regs[reg] = (ci->regs[reg]&(~mask))|val;
196
ci->lastaddress = reg;
197
status = i2c_write_reg(ci->i2c, ci->adr, 1, ci->regs[reg]);
200
ci->regs[reg] &= 0x7f;
204
static int write_reg(struct cxd *ci, u8 reg, u8 val)
206
return write_regm(ci, reg, val, 0xff);
210
static int write_block(struct cxd *ci, u8 adr, u8 *data, int n)
215
status = i2c_write_reg(ci->i2c, ci->adr, 0, adr);
217
ci->lastaddress = adr;
218
memcpy(buf+1, data, n);
219
status = i2c_write(ci->i2c, ci->adr, buf, n+1);
225
static void set_mode(struct cxd *ci, int mode)
227
if (mode == ci->mode)
231
case 0x00: /* IO mem */
232
write_regm(ci, 0x06, 0x00, 0x07);
234
case 0x01: /* ATT mem */
235
write_regm(ci, 0x06, 0x02, 0x07);
243
static void cam_mode(struct cxd *ci, int mode)
245
if (mode == ci->cammode)
250
write_regm(ci, 0x20, 0x80, 0x80);
253
printk(KERN_INFO "enable cam buffer mode\n");
254
/* write_reg(ci, 0x0d, 0x00); */
255
/* write_reg(ci, 0x0e, 0x01); */
256
write_regm(ci, 0x08, 0x40, 0x40);
257
/* read_reg(ci, 0x12, &dummy); */
258
write_regm(ci, 0x08, 0x80, 0x80);
268
#define CHK_ERROR(s) if ((status = s)) break
270
static int init(struct cxd *ci)
274
mutex_lock(&ci->lock);
277
CHK_ERROR(write_reg(ci, 0x00, 0x00));
278
CHK_ERROR(write_reg(ci, 0x01, 0x00));
279
CHK_ERROR(write_reg(ci, 0x02, 0x10));
280
CHK_ERROR(write_reg(ci, 0x03, 0x00));
281
CHK_ERROR(write_reg(ci, 0x05, 0xFF));
282
CHK_ERROR(write_reg(ci, 0x06, 0x1F));
283
CHK_ERROR(write_reg(ci, 0x07, 0x1F));
284
CHK_ERROR(write_reg(ci, 0x08, 0x28));
285
CHK_ERROR(write_reg(ci, 0x14, 0x20));
287
CHK_ERROR(write_reg(ci, 0x09, 0x4D)); /* Input Mode C, BYPass Serial, TIVAL = low, MSB */
288
CHK_ERROR(write_reg(ci, 0x0A, 0xA7)); /* TOSTRT = 8, Mode B (gated clock), falling Edge, Serial, POL=HIGH, MSB */
291
CHK_ERROR(write_reg(ci, 0x0B, 0x33));
292
CHK_ERROR(write_reg(ci, 0x0C, 0x33));
294
CHK_ERROR(write_regm(ci, 0x14, 0x00, 0x0F));
295
CHK_ERROR(write_reg(ci, 0x15, ci->clk_reg_b));
296
CHK_ERROR(write_regm(ci, 0x16, 0x00, 0x0F));
297
CHK_ERROR(write_reg(ci, 0x17, ci->clk_reg_f));
299
CHK_ERROR(write_reg(ci, 0x20, 0x28)); /* Integer Divider, Falling Edge, Internal Sync, */
300
CHK_ERROR(write_reg(ci, 0x21, 0x00)); /* MCLKI = TICLK/8 */
301
CHK_ERROR(write_reg(ci, 0x22, 0x07)); /* MCLKI = TICLK/8 */
304
CHK_ERROR(write_regm(ci, 0x20, 0x80, 0x80)); /* Reset CAM state machine */
306
CHK_ERROR(write_regm(ci, 0x03, 0x02, 02)); /* Enable IREQA Interrupt */
307
CHK_ERROR(write_reg(ci, 0x01, 0x04)); /* Enable CD Interrupt */
308
CHK_ERROR(write_reg(ci, 0x00, 0x31)); /* Enable TS1,Hot Swap,Slot A */
309
CHK_ERROR(write_regm(ci, 0x09, 0x08, 0x08)); /* Put TS in bypass */
315
mutex_unlock(&ci->lock);
321
static int read_attribute_mem(struct dvb_ca_en50221 *ca,
322
int slot, int address)
324
struct cxd *ci = ca->data;
326
mutex_lock(&ci->lock);
328
read_pccard(ci, address, &val, 1);
329
mutex_unlock(&ci->lock);
334
static int write_attribute_mem(struct dvb_ca_en50221 *ca, int slot,
335
int address, u8 value)
337
struct cxd *ci = ca->data;
339
mutex_lock(&ci->lock);
341
write_pccard(ci, address, &value, 1);
342
mutex_unlock(&ci->lock);
346
static int read_cam_control(struct dvb_ca_en50221 *ca,
347
int slot, u8 address)
349
struct cxd *ci = ca->data;
352
mutex_lock(&ci->lock);
354
read_io(ci, address, &val);
355
mutex_unlock(&ci->lock);
359
static int write_cam_control(struct dvb_ca_en50221 *ca, int slot,
360
u8 address, u8 value)
362
struct cxd *ci = ca->data;
364
mutex_lock(&ci->lock);
366
write_io(ci, address, value);
367
mutex_unlock(&ci->lock);
371
static int slot_reset(struct dvb_ca_en50221 *ca, int slot)
373
struct cxd *ci = ca->data;
375
mutex_lock(&ci->lock);
377
write_reg(ci, 0x00, 0x21);
378
write_reg(ci, 0x06, 0x1F);
379
write_reg(ci, 0x00, 0x31);
380
write_regm(ci, 0x20, 0x80, 0x80);
381
write_reg(ci, 0x03, 0x02);
386
for (i = 0; i < 100; i++) {
392
mutex_unlock(&ci->lock);
397
static int slot_shutdown(struct dvb_ca_en50221 *ca, int slot)
399
struct cxd *ci = ca->data;
401
printk(KERN_INFO "slot_shutdown\n");
402
mutex_lock(&ci->lock);
403
/* write_regm(ci, 0x09, 0x08, 0x08); */
404
write_regm(ci, 0x20, 0x80, 0x80);
405
write_regm(ci, 0x06, 0x07, 0x07);
407
mutex_unlock(&ci->lock);
408
return 0; /* shutdown(ci); */
411
static int slot_ts_enable(struct dvb_ca_en50221 *ca, int slot)
413
struct cxd *ci = ca->data;
415
mutex_lock(&ci->lock);
416
write_regm(ci, 0x09, 0x00, 0x08);
421
mutex_unlock(&ci->lock);
426
static int campoll(struct cxd *ci)
430
read_reg(ci, 0x04, &istat);
433
write_reg(ci, 0x05, istat);
437
printk(KERN_INFO "DR\n");
440
printk(KERN_INFO "WC\n");
445
read_reg(ci, 0x01, &slotstat);
447
if (!ci->slot_stat) {
448
ci->slot_stat |= DVB_CA_EN50221_POLL_CAM_PRESENT;
449
write_regm(ci, 0x03, 0x08, 0x08);
455
write_regm(ci, 0x03, 0x00, 0x08);
456
printk(KERN_INFO "NO CAM\n");
460
if (istat&8 && ci->slot_stat == DVB_CA_EN50221_POLL_CAM_PRESENT) {
462
ci->slot_stat |= DVB_CA_EN50221_POLL_CAM_READY;
463
printk(KERN_INFO "READY\n");
470
static int poll_slot_status(struct dvb_ca_en50221 *ca, int slot, int open)
472
struct cxd *ci = ca->data;
475
mutex_lock(&ci->lock);
477
read_reg(ci, 0x01, &slotstat);
478
mutex_unlock(&ci->lock);
480
return ci->slot_stat;
484
static int read_data(struct dvb_ca_en50221 *ca, int slot, u8 *ebuf, int ecount)
486
struct cxd *ci = ca->data;
490
mutex_lock(&ci->lock);
492
mutex_unlock(&ci->lock);
494
printk(KERN_INFO "read_data\n");
498
mutex_lock(&ci->lock);
499
read_reg(ci, 0x0f, &msb);
500
read_reg(ci, 0x10, &lsb);
502
read_block(ci, 0x12, ebuf, len);
504
mutex_unlock(&ci->lock);
509
static int write_data(struct dvb_ca_en50221 *ca, int slot, u8 *ebuf, int ecount)
511
struct cxd *ci = ca->data;
513
mutex_lock(&ci->lock);
514
printk(KERN_INFO "write_data %d\n", ecount);
515
write_reg(ci, 0x0d, ecount>>8);
516
write_reg(ci, 0x0e, ecount&0xff);
517
write_block(ci, 0x11, ebuf, ecount);
518
mutex_unlock(&ci->lock);
523
static struct dvb_ca_en50221 en_templ = {
524
.read_attribute_mem = read_attribute_mem,
525
.write_attribute_mem = write_attribute_mem,
526
.read_cam_control = read_cam_control,
527
.write_cam_control = write_cam_control,
528
.slot_reset = slot_reset,
529
.slot_shutdown = slot_shutdown,
530
.slot_ts_enable = slot_ts_enable,
531
.poll_slot_status = poll_slot_status,
533
.read_data = read_data,
534
.write_data = write_data,
539
struct dvb_ca_en50221 *cxd2099_attach(u8 adr, void *priv,
540
struct i2c_adapter *i2c)
543
u32 bitrate = 62000000;
546
if (i2c_read_reg(i2c, adr, 0, &val) < 0) {
547
printk(KERN_ERR "No CXD2099 detected at %02x\n", adr);
551
ci = kmalloc(sizeof(struct cxd), GFP_KERNEL);
554
memset(ci, 0, sizeof(*ci));
556
mutex_init(&ci->lock);
559
ci->lastaddress = 0xff;
560
ci->clk_reg_b = 0x4a;
561
ci->clk_reg_f = 0x1b;
562
ci->bitrate = bitrate;
564
memcpy(&ci->en, &en_templ, sizeof(en_templ));
567
printk(KERN_INFO "Attached CXD2099AR at %02x\n", ci->adr);
570
EXPORT_SYMBOL(cxd2099_attach);
572
MODULE_DESCRIPTION("cxd2099");
573
MODULE_AUTHOR("Ralph Metzler <rjkm@metzlerbros.de>");
574
MODULE_LICENSE("GPL");