2 * cxd2099.c: Driver for the CXD2099AR Common Interface Controller
4 * Copyright (C) 2010-2011 Digital Devices GmbH
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/slab.h>
26 #include <linux/kernel.h>
27 #include <linux/module.h>
28 #include <linux/i2c.h>
29 #include <linux/wait.h>
30 #include <linux/delay.h>
31 #include <linux/mutex.h>
36 #define MAX_BUFFER_SIZE 248
39 struct dvb_ca_en50221 en
;
41 struct i2c_adapter
*i2c
;
42 struct cxd2099_cfg cfg
;
60 static int i2c_write_reg(struct i2c_adapter
*adapter
, u8 adr
,
63 u8 m
[2] = {reg
, data
};
64 struct i2c_msg msg
= {.addr
= adr
, .flags
= 0, .buf
= m
, .len
= 2};
66 if (i2c_transfer(adapter
, &msg
, 1) != 1) {
67 dev_err(&adapter
->dev
,
68 "Failed to write to I2C register %02x@%02x!\n",
75 static int i2c_write(struct i2c_adapter
*adapter
, u8 adr
,
78 struct i2c_msg msg
= {.addr
= adr
, .flags
= 0, .buf
= data
, .len
= len
};
80 if (i2c_transfer(adapter
, &msg
, 1) != 1) {
81 dev_err(&adapter
->dev
, "Failed to write to I2C!\n");
87 static int i2c_read_reg(struct i2c_adapter
*adapter
, u8 adr
,
90 struct i2c_msg msgs
[2] = {{.addr
= adr
, .flags
= 0,
91 .buf
= ®
, .len
= 1},
92 {.addr
= adr
, .flags
= I2C_M_RD
,
93 .buf
= val
, .len
= 1} };
95 if (i2c_transfer(adapter
, msgs
, 2) != 2) {
96 dev_err(&adapter
->dev
, "error in i2c_read_reg\n");
102 static int i2c_read(struct i2c_adapter
*adapter
, u8 adr
,
103 u8 reg
, u8
*data
, u8 n
)
105 struct i2c_msg msgs
[2] = {{.addr
= adr
, .flags
= 0,
106 .buf
= ®
, .len
= 1},
107 {.addr
= adr
, .flags
= I2C_M_RD
,
108 .buf
= data
, .len
= n
} };
110 if (i2c_transfer(adapter
, msgs
, 2) != 2) {
111 dev_err(&adapter
->dev
, "error in i2c_read\n");
117 static int read_block(struct cxd
*ci
, u8 adr
, u8
*data
, u8 n
)
121 status
= i2c_write_reg(ci
->i2c
, ci
->cfg
.adr
, 0, adr
);
123 ci
->lastaddress
= adr
;
124 status
= i2c_read(ci
->i2c
, ci
->cfg
.adr
, 1, data
, n
);
129 static int read_reg(struct cxd
*ci
, u8 reg
, u8
*val
)
131 return read_block(ci
, reg
, val
, 1);
134 static int read_pccard(struct cxd
*ci
, u16 address
, u8
*data
, u8 n
)
137 u8 addr
[3] = {2, address
& 0xff, address
>> 8};
139 status
= i2c_write(ci
->i2c
, ci
->cfg
.adr
, addr
, 3);
141 status
= i2c_read(ci
->i2c
, ci
->cfg
.adr
, 3, data
, n
);
145 static int write_pccard(struct cxd
*ci
, u16 address
, u8
*data
, u8 n
)
148 u8 addr
[3] = {2, address
& 0xff, address
>> 8};
150 status
= i2c_write(ci
->i2c
, ci
->cfg
.adr
, addr
, 3);
154 memcpy(buf
+ 1, data
, n
);
155 status
= i2c_write(ci
->i2c
, ci
->cfg
.adr
, buf
, n
+ 1);
160 static int read_io(struct cxd
*ci
, u16 address
, u8
*val
)
163 u8 addr
[3] = {2, address
& 0xff, address
>> 8};
165 status
= i2c_write(ci
->i2c
, ci
->cfg
.adr
, addr
, 3);
167 status
= i2c_read(ci
->i2c
, ci
->cfg
.adr
, 3, val
, 1);
171 static int write_io(struct cxd
*ci
, u16 address
, u8 val
)
174 u8 addr
[3] = {2, address
& 0xff, address
>> 8};
175 u8 buf
[2] = {3, val
};
177 status
= i2c_write(ci
->i2c
, ci
->cfg
.adr
, addr
, 3);
179 status
= i2c_write(ci
->i2c
, ci
->cfg
.adr
, buf
, 2);
183 static int write_regm(struct cxd
*ci
, u8 reg
, u8 val
, u8 mask
)
187 status
= i2c_write_reg(ci
->i2c
, ci
->cfg
.adr
, 0, reg
);
188 if (!status
&& reg
>= 6 && reg
<= 8 && mask
!= 0xff)
189 status
= i2c_read_reg(ci
->i2c
, ci
->cfg
.adr
, 1, &ci
->regs
[reg
]);
190 ci
->regs
[reg
] = (ci
->regs
[reg
] & (~mask
)) | val
;
192 ci
->lastaddress
= reg
;
193 status
= i2c_write_reg(ci
->i2c
, ci
->cfg
.adr
, 1, ci
->regs
[reg
]);
196 ci
->regs
[reg
] &= 0x7f;
200 static int write_reg(struct cxd
*ci
, u8 reg
, u8 val
)
202 return write_regm(ci
, reg
, val
, 0xff);
206 static int write_block(struct cxd
*ci
, u8 adr
, u8
*data
, int n
)
211 status
= i2c_write_reg(ci
->i2c
, ci
->cfg
.adr
, 0, adr
);
213 ci
->lastaddress
= adr
;
214 memcpy(buf
+ 1, data
, n
);
215 status
= i2c_write(ci
->i2c
, ci
->cfg
.adr
, buf
, n
+ 1);
221 static void set_mode(struct cxd
*ci
, int mode
)
223 if (mode
== ci
->mode
)
227 case 0x00: /* IO mem */
228 write_regm(ci
, 0x06, 0x00, 0x07);
230 case 0x01: /* ATT mem */
231 write_regm(ci
, 0x06, 0x02, 0x07);
239 static void cam_mode(struct cxd
*ci
, int mode
)
241 if (mode
== ci
->cammode
)
246 write_regm(ci
, 0x20, 0x80, 0x80);
250 if (!ci
->en
.read_data
)
252 dev_info(&ci
->i2c
->dev
, "enable cam buffer mode\n");
253 /* write_reg(ci, 0x0d, 0x00); */
254 /* write_reg(ci, 0x0e, 0x01); */
255 write_regm(ci
, 0x08, 0x40, 0x40);
256 /* read_reg(ci, 0x12, &dummy); */
257 write_regm(ci
, 0x08, 0x80, 0x80);
266 static int init(struct cxd
*ci
)
270 mutex_lock(&ci
->lock
);
273 status
= write_reg(ci
, 0x00, 0x00);
276 status
= write_reg(ci
, 0x01, 0x00);
279 status
= write_reg(ci
, 0x02, 0x10);
282 status
= write_reg(ci
, 0x03, 0x00);
285 status
= write_reg(ci
, 0x05, 0xFF);
288 status
= write_reg(ci
, 0x06, 0x1F);
291 status
= write_reg(ci
, 0x07, 0x1F);
294 status
= write_reg(ci
, 0x08, 0x28);
297 status
= write_reg(ci
, 0x14, 0x20);
301 /* TOSTRT = 8, Mode B (gated clock), falling Edge,
302 * Serial, POL=HIGH, MSB
304 status
= write_reg(ci
, 0x0A, 0xA7);
308 status
= write_reg(ci
, 0x0B, 0x33);
311 status
= write_reg(ci
, 0x0C, 0x33);
315 status
= write_regm(ci
, 0x14, 0x00, 0x0F);
318 status
= write_reg(ci
, 0x15, ci
->clk_reg_b
);
321 status
= write_regm(ci
, 0x16, 0x00, 0x0F);
324 status
= write_reg(ci
, 0x17, ci
->clk_reg_f
);
328 if (ci
->cfg
.clock_mode
) {
329 if (ci
->cfg
.polarity
) {
330 status
= write_reg(ci
, 0x09, 0x6f);
334 status
= write_reg(ci
, 0x09, 0x6d);
338 status
= write_reg(ci
, 0x20, 0x68);
341 status
= write_reg(ci
, 0x21, 0x00);
344 status
= write_reg(ci
, 0x22, 0x02);
348 if (ci
->cfg
.polarity
) {
349 status
= write_reg(ci
, 0x09, 0x4f);
353 status
= write_reg(ci
, 0x09, 0x4d);
358 status
= write_reg(ci
, 0x20, 0x28);
361 status
= write_reg(ci
, 0x21, 0x00);
364 status
= write_reg(ci
, 0x22, 0x07);
369 status
= write_regm(ci
, 0x20, 0x80, 0x80);
372 status
= write_regm(ci
, 0x03, 0x02, 0x02);
375 status
= write_reg(ci
, 0x01, 0x04);
378 status
= write_reg(ci
, 0x00, 0x31);
382 /* Put TS in bypass */
383 status
= write_regm(ci
, 0x09, 0x08, 0x08);
389 mutex_unlock(&ci
->lock
);
394 static int read_attribute_mem(struct dvb_ca_en50221
*ca
,
395 int slot
, int address
)
397 struct cxd
*ci
= ca
->data
;
400 mutex_lock(&ci
->lock
);
402 read_pccard(ci
, address
, &val
, 1);
403 mutex_unlock(&ci
->lock
);
404 /* printk(KERN_INFO "%02x:%02x\n", address,val); */
408 static int write_attribute_mem(struct dvb_ca_en50221
*ca
, int slot
,
409 int address
, u8 value
)
411 struct cxd
*ci
= ca
->data
;
413 mutex_lock(&ci
->lock
);
415 write_pccard(ci
, address
, &value
, 1);
416 mutex_unlock(&ci
->lock
);
420 static int read_cam_control(struct dvb_ca_en50221
*ca
,
421 int slot
, u8 address
)
423 struct cxd
*ci
= ca
->data
;
426 mutex_lock(&ci
->lock
);
428 read_io(ci
, address
, &val
);
429 mutex_unlock(&ci
->lock
);
433 static int write_cam_control(struct dvb_ca_en50221
*ca
, int slot
,
434 u8 address
, u8 value
)
436 struct cxd
*ci
= ca
->data
;
438 mutex_lock(&ci
->lock
);
440 write_io(ci
, address
, value
);
441 mutex_unlock(&ci
->lock
);
445 static int slot_reset(struct dvb_ca_en50221
*ca
, int slot
)
447 struct cxd
*ci
= ca
->data
;
449 mutex_lock(&ci
->lock
);
451 write_reg(ci
, 0x00, 0x21);
452 write_reg(ci
, 0x06, 0x1F);
453 write_reg(ci
, 0x00, 0x31);
454 write_regm(ci
, 0x20, 0x80, 0x80);
455 write_reg(ci
, 0x03, 0x02);
461 for (i
= 0; i
< 100; i
++) {
462 usleep_range(10000, 11000);
467 mutex_unlock(&ci
->lock
);
472 static int slot_shutdown(struct dvb_ca_en50221
*ca
, int slot
)
474 struct cxd
*ci
= ca
->data
;
476 dev_info(&ci
->i2c
->dev
, "%s\n", __func__
);
477 mutex_lock(&ci
->lock
);
478 write_regm(ci
, 0x09, 0x08, 0x08);
479 write_regm(ci
, 0x20, 0x80, 0x80); /* Reset CAM Mode */
480 write_regm(ci
, 0x06, 0x07, 0x07); /* Clear IO Mode */
482 mutex_unlock(&ci
->lock
);
486 static int slot_ts_enable(struct dvb_ca_en50221
*ca
, int slot
)
488 struct cxd
*ci
= ca
->data
;
490 mutex_lock(&ci
->lock
);
491 write_regm(ci
, 0x09, 0x00, 0x08);
496 mutex_unlock(&ci
->lock
);
500 static int campoll(struct cxd
*ci
)
504 read_reg(ci
, 0x04, &istat
);
507 write_reg(ci
, 0x05, istat
);
511 dev_info(&ci
->i2c
->dev
, "DR\n");
514 dev_info(&ci
->i2c
->dev
, "WC\n");
519 read_reg(ci
, 0x01, &slotstat
);
520 if (!(2 & slotstat
)) {
521 if (!ci
->slot_stat
) {
522 ci
->slot_stat
= DVB_CA_EN50221_POLL_CAM_PRESENT
;
523 write_regm(ci
, 0x03, 0x08, 0x08);
529 write_regm(ci
, 0x03, 0x00, 0x08);
530 dev_info(&ci
->i2c
->dev
, "NO CAM\n");
535 ci
->slot_stat
== DVB_CA_EN50221_POLL_CAM_PRESENT
) {
537 ci
->slot_stat
|= DVB_CA_EN50221_POLL_CAM_READY
;
543 static int poll_slot_status(struct dvb_ca_en50221
*ca
, int slot
, int open
)
545 struct cxd
*ci
= ca
->data
;
548 mutex_lock(&ci
->lock
);
550 read_reg(ci
, 0x01, &slotstat
);
551 mutex_unlock(&ci
->lock
);
553 return ci
->slot_stat
;
557 static int read_data(struct dvb_ca_en50221
*ca
, int slot
, u8
*ebuf
, int ecount
)
559 struct cxd
*ci
= ca
->data
;
563 mutex_lock(&ci
->lock
);
565 mutex_unlock(&ci
->lock
);
567 dev_info(&ci
->i2c
->dev
, "%s\n", __func__
);
571 mutex_lock(&ci
->lock
);
572 read_reg(ci
, 0x0f, &msb
);
573 read_reg(ci
, 0x10, &lsb
);
574 len
= (msb
<< 8) | lsb
;
575 read_block(ci
, 0x12, ebuf
, len
);
577 mutex_unlock(&ci
->lock
);
582 static int write_data(struct dvb_ca_en50221
*ca
, int slot
, u8
*ebuf
, int ecount
)
584 struct cxd
*ci
= ca
->data
;
586 mutex_lock(&ci
->lock
);
587 dev_info(&ci
->i2c
->dev
, "%s %d\n", __func__
, ecount
);
588 write_reg(ci
, 0x0d, ecount
>> 8);
589 write_reg(ci
, 0x0e, ecount
& 0xff);
590 write_block(ci
, 0x11, ebuf
, ecount
);
591 mutex_unlock(&ci
->lock
);
596 static struct dvb_ca_en50221 en_templ
= {
597 .read_attribute_mem
= read_attribute_mem
,
598 .write_attribute_mem
= write_attribute_mem
,
599 .read_cam_control
= read_cam_control
,
600 .write_cam_control
= write_cam_control
,
601 .slot_reset
= slot_reset
,
602 .slot_shutdown
= slot_shutdown
,
603 .slot_ts_enable
= slot_ts_enable
,
604 .poll_slot_status
= poll_slot_status
,
606 .read_data
= read_data
,
607 .write_data
= write_data
,
612 struct dvb_ca_en50221
*cxd2099_attach(struct cxd2099_cfg
*cfg
,
614 struct i2c_adapter
*i2c
)
619 if (i2c_read_reg(i2c
, cfg
->adr
, 0, &val
) < 0) {
620 dev_info(&i2c
->dev
, "No CXD2099 detected at %02x\n", cfg
->adr
);
624 ci
= kzalloc(sizeof(*ci
), GFP_KERNEL
);
628 mutex_init(&ci
->lock
);
631 ci
->lastaddress
= 0xff;
632 ci
->clk_reg_b
= 0x4a;
633 ci
->clk_reg_f
= 0x1b;
638 dev_info(&i2c
->dev
, "Attached CXD2099AR at %02x\n", ci
->cfg
.adr
);
641 EXPORT_SYMBOL(cxd2099_attach
);
643 MODULE_DESCRIPTION("cxd2099");
644 MODULE_AUTHOR("Ralph Metzler");
645 MODULE_LICENSE("GPL");