4 * CIMax2(R) SP2 driver in conjunction with NetUp Dual DVB-S2 CI card
6 * Copyright (C) 2009 NetUP Inc.
7 * Copyright (C) 2009 Igor M. Liplianin <liplianin@netup.ru>
8 * Copyright (C) 2009 Abylay Ospan <aospan@netup.ru>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
24 #include "dvb_ca_en50221.h"
26 /* Max transfer size done by I2C transfer functions */
27 #define MAX_XFER_SIZE 64
29 /**** Bit definitions for MC417_RWD and MC417_OEN registers ***
34 bit 15 bit 14 bit 13 bit 12 bit 11 bit 10 bit 9 bit 8
35 +-------+-------+-------+-------+-------+-------+-------+-------+
36 | WR# | RD# | | ACK# | ADHI | ADLO | CS1# | CS0# |
37 +-------+-------+-------+-------+-------+-------+-------+-------+
38 bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0
39 +-------+-------+-------+-------+-------+-------+-------+-------+
40 | DATA7| DATA6| DATA5| DATA4| DATA3| DATA2| DATA1| DATA0|
41 +-------+-------+-------+-------+-------+-------+-------+-------+
44 #define NETUP_DATA 0x000000ff
45 #define NETUP_WR 0x00008000
46 #define NETUP_RD 0x00004000
47 #define NETUP_ACK 0x00001000
48 #define NETUP_ADHI 0x00000800
49 #define NETUP_ADLO 0x00000400
50 #define NETUP_CS1 0x00000200
51 #define NETUP_CS0 0x00000100
52 #define NETUP_EN_ALL 0x00001000
53 #define NETUP_CTRL_OFF (NETUP_CS1 | NETUP_CS0 | NETUP_WR | NETUP_RD)
54 #define NETUP_CI_CTL 0x04
57 #define NETUP_IRQ_DETAM 0x1
58 #define NETUP_IRQ_IRQAM 0x4
60 static unsigned int ci_dbg
;
61 module_param(ci_dbg
, int, 0644);
62 MODULE_PARM_DESC(ci_dbg
, "Enable CI debugging");
64 static unsigned int ci_irq_enable
;
65 module_param(ci_irq_enable
, int, 0644);
66 MODULE_PARM_DESC(ci_irq_enable
, "Enable IRQ from CAM");
68 #define ci_dbg_print(args...) \
71 printk(KERN_DEBUG args); \
74 #define ci_irq_flags() (ci_irq_enable ? NETUP_IRQ_IRQAM : 0)
76 /* stores all private variables for communication with CI */
77 struct netup_ci_state
{
78 struct dvb_ca_en50221 ca
;
79 struct mutex ca_mutex
;
80 struct i2c_adapter
*i2c_adap
;
83 struct work_struct work
;
87 unsigned long next_status_checked_time
;
91 static int netup_read_i2c(struct i2c_adapter
*i2c_adap
, u8 addr
, u8 reg
,
95 struct i2c_msg msg
[] = {
109 ret
= i2c_transfer(i2c_adap
, msg
, 2);
112 ci_dbg_print("%s: i2c read error, Reg = 0x%02x, Status = %d\n",
118 ci_dbg_print("%s: i2c read Addr=0x%04x, Reg = 0x%02x, data = %02x\n",
119 __func__
, addr
, reg
, buf
[0]);
124 static int netup_write_i2c(struct i2c_adapter
*i2c_adap
, u8 addr
, u8 reg
,
128 u8 buffer
[MAX_XFER_SIZE
];
130 struct i2c_msg msg
= {
137 if (1 + len
> sizeof(buffer
)) {
139 "%s: i2c wr reg=%04x: len=%d is too big!\n",
140 KBUILD_MODNAME
, reg
, len
);
145 memcpy(&buffer
[1], buf
, len
);
147 ret
= i2c_transfer(i2c_adap
, &msg
, 1);
150 ci_dbg_print("%s: i2c write error, Reg=[0x%02x], Status=%d\n",
158 static int netup_ci_get_mem(struct cx23885_dev
*dev
)
161 unsigned long timeout
= jiffies
+ msecs_to_jiffies(1);
164 mem
= cx_read(MC417_RWD
);
165 if ((mem
& NETUP_ACK
) == 0)
167 if (time_after(jiffies
, timeout
))
172 cx_set(MC417_RWD
, NETUP_CTRL_OFF
);
177 static int netup_ci_op_cam(struct dvb_ca_en50221
*en50221
, int slot
,
178 u8 flag
, u8 read
, int addr
, u8 data
)
180 struct netup_ci_state
*state
= en50221
->data
;
181 struct cx23885_tsport
*port
= state
->priv
;
182 struct cx23885_dev
*dev
= port
->dev
;
191 if (state
->current_ci_flag
!= flag
) {
192 ret
= netup_read_i2c(state
->i2c_adap
, state
->ci_i2c_addr
,
200 ret
= netup_write_i2c(state
->i2c_adap
, state
->ci_i2c_addr
,
205 state
->current_ci_flag
= flag
;
207 mutex_lock(&dev
->gpio_lock
);
210 cx_write(MC417_OEN
, NETUP_EN_ALL
);
211 cx_write(MC417_RWD
, NETUP_CTRL_OFF
|
212 NETUP_ADLO
| (0xff & addr
));
213 cx_clear(MC417_RWD
, NETUP_ADLO
);
214 cx_write(MC417_RWD
, NETUP_CTRL_OFF
|
215 NETUP_ADHI
| (0xff & (addr
>> 8)));
216 cx_clear(MC417_RWD
, NETUP_ADHI
);
218 if (read
) { /* data in */
219 cx_write(MC417_OEN
, NETUP_EN_ALL
| NETUP_DATA
);
220 } else /* data out */
221 cx_write(MC417_RWD
, NETUP_CTRL_OFF
| data
);
225 (state
->ci_i2c_addr
== 0x40) ? NETUP_CS0
: NETUP_CS1
);
227 cx_clear(MC417_RWD
, (read
) ? NETUP_RD
: NETUP_WR
);
228 mem
= netup_ci_get_mem(dev
);
230 mutex_unlock(&dev
->gpio_lock
);
236 ci_dbg_print("%s: %s: chipaddr=[0x%x] addr=[0x%02x], %s=%x\n", __func__
,
237 (read
) ? "read" : "write", state
->ci_i2c_addr
, addr
,
238 (flag
== NETUP_CI_CTL
) ? "ctl" : "mem",
239 (read
) ? mem
: data
);
247 int netup_ci_read_attribute_mem(struct dvb_ca_en50221
*en50221
,
250 return netup_ci_op_cam(en50221
, slot
, 0, NETUP_CI_RD
, addr
, 0);
253 int netup_ci_write_attribute_mem(struct dvb_ca_en50221
*en50221
,
254 int slot
, int addr
, u8 data
)
256 return netup_ci_op_cam(en50221
, slot
, 0, 0, addr
, data
);
259 int netup_ci_read_cam_ctl(struct dvb_ca_en50221
*en50221
, int slot
,
262 return netup_ci_op_cam(en50221
, slot
, NETUP_CI_CTL
,
263 NETUP_CI_RD
, addr
, 0);
266 int netup_ci_write_cam_ctl(struct dvb_ca_en50221
*en50221
, int slot
,
269 return netup_ci_op_cam(en50221
, slot
, NETUP_CI_CTL
, 0, addr
, data
);
272 int netup_ci_slot_reset(struct dvb_ca_en50221
*en50221
, int slot
)
274 struct netup_ci_state
*state
= en50221
->data
;
282 ret
= netup_write_i2c(state
->i2c_adap
, state
->ci_i2c_addr
,
291 ret
= netup_write_i2c(state
->i2c_adap
, state
->ci_i2c_addr
,
295 dvb_ca_en50221_camready_irq(&state
->ca
, 0);
301 int netup_ci_slot_shutdown(struct dvb_ca_en50221
*en50221
, int slot
)
303 /* not implemented */
307 static int netup_ci_set_irq(struct dvb_ca_en50221
*en50221
, u8 irq_mode
)
309 struct netup_ci_state
*state
= en50221
->data
;
312 if (irq_mode
== state
->current_irq_mode
)
315 ci_dbg_print("%s: chipaddr=[0x%x] setting ci IRQ to [0x%x] \n",
316 __func__
, state
->ci_i2c_addr
, irq_mode
);
317 ret
= netup_write_i2c(state
->i2c_adap
, state
->ci_i2c_addr
,
323 state
->current_irq_mode
= irq_mode
;
328 int netup_ci_slot_ts_ctl(struct dvb_ca_en50221
*en50221
, int slot
)
330 struct netup_ci_state
*state
= en50221
->data
;
336 netup_read_i2c(state
->i2c_adap
, state
->ci_i2c_addr
,
340 return netup_write_i2c(state
->i2c_adap
, state
->ci_i2c_addr
,
345 static void netup_read_ci_status(struct work_struct
*work
)
347 struct netup_ci_state
*state
=
348 container_of(work
, struct netup_ci_state
, work
);
352 /* CAM module IRQ processing. fast operation */
353 dvb_ca_en50221_frda_irq(&state
->ca
, 0);
355 /* CAM module INSERT/REMOVE processing. slow operation because of i2c
357 if (time_after(jiffies
, state
->next_status_checked_time
)
359 ret
= netup_read_i2c(state
->i2c_adap
, state
->ci_i2c_addr
,
362 state
->next_status_checked_time
= jiffies
363 + msecs_to_jiffies(1000);
368 ci_dbg_print("%s: Slot Status Addr=[0x%04x], "
369 "Reg=[0x%02x], data=%02x, "
370 "TS config = %02x\n", __func__
,
371 state
->ci_i2c_addr
, 0, buf
[0],
376 state
->status
= DVB_CA_EN50221_POLL_CAM_PRESENT
|
377 DVB_CA_EN50221_POLL_CAM_READY
;
384 int netup_ci_slot_status(struct cx23885_dev
*dev
, u32 pci_status
)
386 struct cx23885_tsport
*port
= NULL
;
387 struct netup_ci_state
*state
= NULL
;
389 ci_dbg_print("%s:\n", __func__
);
391 if (0 == (pci_status
& (PCI_MSK_GPIO0
| PCI_MSK_GPIO1
)))
394 if (pci_status
& PCI_MSK_GPIO0
) {
396 state
= port
->port_priv
;
397 schedule_work(&state
->work
);
398 ci_dbg_print("%s: Wakeup CI0\n", __func__
);
401 if (pci_status
& PCI_MSK_GPIO1
) {
403 state
= port
->port_priv
;
404 schedule_work(&state
->work
);
405 ci_dbg_print("%s: Wakeup CI1\n", __func__
);
411 int netup_poll_ci_slot_status(struct dvb_ca_en50221
*en50221
,
414 struct netup_ci_state
*state
= en50221
->data
;
419 netup_ci_set_irq(en50221
, open
? (NETUP_IRQ_DETAM
| ci_irq_flags())
422 return state
->status
;
425 int netup_ci_init(struct cx23885_tsport
*port
)
427 struct netup_ci_state
*state
;
428 u8 cimax_init
[34] = {
429 0x00, /* module A control*/
430 0x00, /* auto select mask high A */
431 0x00, /* auto select mask low A */
432 0x00, /* auto select pattern high A */
433 0x00, /* auto select pattern low A */
434 0x44, /* memory access time A */
435 0x00, /* invert input A */
438 0x00, /* module B control*/
439 0x00, /* auto select mask high B */
440 0x00, /* auto select mask low B */
441 0x00, /* auto select pattern high B */
442 0x00, /* auto select pattern low B */
443 0x44, /* memory access time B */
444 0x00, /* invert input B */
447 0x00, /* auto select mask high Ext */
448 0x00, /* auto select mask low Ext */
449 0x00, /* auto select pattern high Ext */
450 0x00, /* auto select pattern low Ext */
452 0x02, /* destination - module A */
453 0x01, /* power on (use it like store place) */
455 0x00, /* int status read only */
456 ci_irq_flags() | NETUP_IRQ_DETAM
, /* DETAM, IRQAM unmasked */
457 0x05, /* EXTINT=active-high, INT=push-pull */
459 0x04, /* ack active low */
461 0x33, /* serial mode, rising in, rising out, MSB first*/
462 0x31, /* synchronization */
466 ci_dbg_print("%s\n", __func__
);
467 state
= kzalloc(sizeof(struct netup_ci_state
), GFP_KERNEL
);
469 ci_dbg_print("%s: Unable create CI structure!\n", __func__
);
474 port
->port_priv
= state
;
478 state
->ci_i2c_addr
= 0x40;
481 state
->ci_i2c_addr
= 0x41;
485 state
->i2c_adap
= &port
->dev
->i2c_bus
[0].i2c_adap
;
486 state
->ca
.owner
= THIS_MODULE
;
487 state
->ca
.read_attribute_mem
= netup_ci_read_attribute_mem
;
488 state
->ca
.write_attribute_mem
= netup_ci_write_attribute_mem
;
489 state
->ca
.read_cam_control
= netup_ci_read_cam_ctl
;
490 state
->ca
.write_cam_control
= netup_ci_write_cam_ctl
;
491 state
->ca
.slot_reset
= netup_ci_slot_reset
;
492 state
->ca
.slot_shutdown
= netup_ci_slot_shutdown
;
493 state
->ca
.slot_ts_enable
= netup_ci_slot_ts_ctl
;
494 state
->ca
.poll_slot_status
= netup_poll_ci_slot_status
;
495 state
->ca
.data
= state
;
497 state
->current_irq_mode
= ci_irq_flags() | NETUP_IRQ_DETAM
;
499 ret
= netup_write_i2c(state
->i2c_adap
, state
->ci_i2c_addr
,
500 0, &cimax_init
[0], 34);
502 ret
|= netup_write_i2c(state
->i2c_adap
, state
->ci_i2c_addr
,
503 0x1f, &cimax_init
[0x18], 1);
505 ret
|= netup_write_i2c(state
->i2c_adap
, state
->ci_i2c_addr
,
506 0x18, &cimax_init
[0x18], 1);
511 ret
= dvb_ca_en50221_init(&port
->frontends
.adapter
,
518 INIT_WORK(&state
->work
, netup_read_ci_status
);
519 schedule_work(&state
->work
);
521 ci_dbg_print("%s: CI initialized!\n", __func__
);
525 ci_dbg_print("%s: Cannot initialize CI: Error %d.\n", __func__
, ret
);
530 void netup_ci_exit(struct cx23885_tsport
*port
)
532 struct netup_ci_state
*state
;
537 state
= (struct netup_ci_state
*)port
->port_priv
;
541 if (NULL
== state
->ca
.data
)
544 dvb_ca_en50221_release(&state
->ca
);