2 * CIMaX SP2/SP2HF (Atmel T90FJR) CI driver
4 * Copyright (C) 2014 Olli Salonen <olli.salonen@iki.fi>
6 * Heavily based on CIMax2(R) SP2 driver in conjunction with NetUp Dual
7 * DVB-S2 CI card (cimax2) with following copyrights:
9 * Copyright (C) 2009 NetUP Inc.
10 * Copyright (C) 2009 Igor M. Liplianin <liplianin@netup.ru>
11 * Copyright (C) 2009 Abylay Ospan <aospan@netup.ru>
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
26 static int sp2_read_i2c(struct sp2
*s
, u8 reg
, u8
*buf
, int len
)
29 struct i2c_client
*client
= s
->client
;
30 struct i2c_adapter
*adap
= client
->adapter
;
31 struct i2c_msg msg
[] = {
45 ret
= i2c_transfer(adap
, msg
, 2);
48 dev_err(&client
->dev
, "i2c read error, reg = 0x%02x, status = %d\n",
56 dev_dbg(&s
->client
->dev
, "addr=0x%04x, reg = 0x%02x, data = %02x\n",
57 client
->addr
, reg
, buf
[0]);
62 static int sp2_write_i2c(struct sp2
*s
, u8 reg
, u8
*buf
, int len
)
66 struct i2c_client
*client
= s
->client
;
67 struct i2c_adapter
*adap
= client
->adapter
;
68 struct i2c_msg msg
= {
75 if ((len
+ 1) > sizeof(buffer
)) {
76 dev_err(&client
->dev
, "i2c wr reg=%02x: len=%d is too big!\n",
82 memcpy(&buffer
[1], buf
, len
);
84 ret
= i2c_transfer(adap
, &msg
, 1);
87 dev_err(&client
->dev
, "i2c write error, reg = 0x%02x, status = %d\n",
95 dev_dbg(&s
->client
->dev
, "addr=0x%04x, reg = 0x%02x, data = %*ph\n",
96 client
->addr
, reg
, len
, buf
);
101 static int sp2_ci_op_cam(struct dvb_ca_en50221
*en50221
, int slot
, u8 acs
,
102 u8 read
, int addr
, u8 data
)
104 struct sp2
*s
= en50221
->data
;
107 int (*ci_op_cam
)(void*, u8
, int, u8
, int*) = s
->ci_control
;
113 * change module access type between IO space and attribute memory
116 if (s
->module_access_type
!= acs
) {
117 ret
= sp2_read_i2c(s
, 0x00, &store
, 1);
122 store
&= ~(SP2_MOD_CTL_ACS1
| SP2_MOD_CTL_ACS0
);
125 ret
= sp2_write_i2c(s
, 0x00, &store
, 1);
130 s
->module_access_type
= acs
;
132 /* implementation of ci_op_cam is device specific */
134 ret
= ci_op_cam(s
->priv
, read
, addr
, data
, &mem
);
136 dev_err(&s
->client
->dev
, "callback not defined");
143 dev_dbg(&s
->client
->dev
, "%s: slot=%d, addr=0x%04x, %s, data=%x",
144 (read
) ? "read" : "write", slot
, addr
,
145 (acs
== SP2_CI_ATTR_ACS
) ? "attr" : "io",
146 (read
) ? mem
: data
);
155 int sp2_ci_read_attribute_mem(struct dvb_ca_en50221
*en50221
,
158 return sp2_ci_op_cam(en50221
, slot
, SP2_CI_ATTR_ACS
,
162 int sp2_ci_write_attribute_mem(struct dvb_ca_en50221
*en50221
,
163 int slot
, int addr
, u8 data
)
165 return sp2_ci_op_cam(en50221
, slot
, SP2_CI_ATTR_ACS
,
166 SP2_CI_WR
, addr
, data
);
169 int sp2_ci_read_cam_control(struct dvb_ca_en50221
*en50221
,
172 return sp2_ci_op_cam(en50221
, slot
, SP2_CI_IO_ACS
,
176 int sp2_ci_write_cam_control(struct dvb_ca_en50221
*en50221
,
177 int slot
, u8 addr
, u8 data
)
179 return sp2_ci_op_cam(en50221
, slot
, SP2_CI_IO_ACS
,
180 SP2_CI_WR
, addr
, data
);
183 int sp2_ci_slot_reset(struct dvb_ca_en50221
*en50221
, int slot
)
185 struct sp2
*s
= en50221
->data
;
189 dev_dbg(&s
->client
->dev
, "slot: %d\n", slot
);
195 buf
= SP2_MOD_CTL_RST
;
196 ret
= sp2_write_i2c(s
, 0x00, &buf
, 1);
201 usleep_range(500, 600);
205 ret
= sp2_write_i2c(s
, 0x00, &buf
, 1);
215 int sp2_ci_slot_shutdown(struct dvb_ca_en50221
*en50221
, int slot
)
217 struct sp2
*s
= en50221
->data
;
219 dev_dbg(&s
->client
->dev
, "slot:%d\n", slot
);
221 /* not implemented */
225 int sp2_ci_slot_ts_enable(struct dvb_ca_en50221
*en50221
, int slot
)
227 struct sp2
*s
= en50221
->data
;
230 dev_dbg(&s
->client
->dev
, "slot:%d\n", slot
);
235 sp2_read_i2c(s
, 0x00, &buf
, 1);
237 /* disable bypass and enable TS */
238 buf
|= (SP2_MOD_CTL_TSOEN
| SP2_MOD_CTL_TSIEN
);
239 return sp2_write_i2c(s
, 0, &buf
, 1);
242 int sp2_ci_poll_slot_status(struct dvb_ca_en50221
*en50221
,
245 struct sp2
*s
= en50221
->data
;
249 dev_dbg(&s
->client
->dev
, "slot:%d open:%d\n", slot
, open
);
252 * CAM module INSERT/REMOVE processing. Slow operation because of i2c
253 * transfers. Throttle read to one per sec.
255 if (time_after(jiffies
, s
->next_status_checked_time
)) {
256 ret
= sp2_read_i2c(s
, 0x00, buf
, 1);
257 s
->next_status_checked_time
= jiffies
+ msecs_to_jiffies(1000);
262 if (buf
[0] & SP2_MOD_CTL_DET
)
263 s
->status
= DVB_CA_EN50221_POLL_CAM_PRESENT
|
264 DVB_CA_EN50221_POLL_CAM_READY
;
272 static int sp2_init(struct sp2
*s
)
276 u8 cimax_init
[34] = {
277 0x00, /* module A control*/
278 0x00, /* auto select mask high A */
279 0x00, /* auto select mask low A */
280 0x00, /* auto select pattern high A */
281 0x00, /* auto select pattern low A */
282 0x44, /* memory access time A, 600 ns */
283 0x00, /* invert input A */
286 0x00, /* module B control*/
287 0x00, /* auto select mask high B */
288 0x00, /* auto select mask low B */
289 0x00, /* auto select pattern high B */
290 0x00, /* auto select pattern low B */
291 0x44, /* memory access time B, 600 ns */
292 0x00, /* invert input B */
295 0x00, /* auto select mask high Ext */
296 0x00, /* auto select mask low Ext */
297 0x00, /* auto select pattern high Ext */
298 0x00, /* auto select pattern low Ext */
300 0x02, /* destination - module A */
301 0x01, /* power control reg, VCC power on */
303 0x00, /* int status read only */
304 0x00, /* Interrupt Mask Register */
305 0x05, /* EXTINT=active-high, INT=push-pull */
307 0x04, /* ack active low */
310 0x00, /* synchronization? */
313 dev_dbg(&s
->client
->dev
, "\n");
315 s
->ca
.owner
= THIS_MODULE
;
316 s
->ca
.read_attribute_mem
= sp2_ci_read_attribute_mem
;
317 s
->ca
.write_attribute_mem
= sp2_ci_write_attribute_mem
;
318 s
->ca
.read_cam_control
= sp2_ci_read_cam_control
;
319 s
->ca
.write_cam_control
= sp2_ci_write_cam_control
;
320 s
->ca
.slot_reset
= sp2_ci_slot_reset
;
321 s
->ca
.slot_shutdown
= sp2_ci_slot_shutdown
;
322 s
->ca
.slot_ts_enable
= sp2_ci_slot_ts_enable
;
323 s
->ca
.poll_slot_status
= sp2_ci_poll_slot_status
;
325 s
->module_access_type
= 0;
327 /* initialize all regs */
328 ret
= sp2_write_i2c(s
, 0x00, &cimax_init
[0], 34);
334 ret
= sp2_write_i2c(s
, 0x1f, &buf
, 1);
339 ret
= sp2_write_i2c(s
, 0x18, &buf
, 1);
343 ret
= dvb_ca_en50221_init(s
->dvb_adap
, &s
->ca
, 0, 1);
350 dev_dbg(&s
->client
->dev
, "init failed=%d\n", ret
);
354 static int sp2_exit(struct i2c_client
*client
)
358 dev_dbg(&client
->dev
, "\n");
363 s
= i2c_get_clientdata(client
);
370 dvb_ca_en50221_release(&s
->ca
);
375 static int sp2_probe(struct i2c_client
*client
,
376 const struct i2c_device_id
*id
)
378 struct sp2_config
*cfg
= client
->dev
.platform_data
;
382 dev_dbg(&client
->dev
, "\n");
384 s
= kzalloc(sizeof(*s
), GFP_KERNEL
);
391 s
->dvb_adap
= cfg
->dvb_adap
;
393 s
->ci_control
= cfg
->ci_control
;
395 i2c_set_clientdata(client
, s
);
401 dev_info(&s
->client
->dev
, "CIMaX SP2 successfully attached\n");
404 dev_dbg(&client
->dev
, "init failed=%d\n", ret
);
410 static int sp2_remove(struct i2c_client
*client
)
412 struct sp2
*s
= i2c_get_clientdata(client
);
414 dev_dbg(&client
->dev
, "\n");
420 static const struct i2c_device_id sp2_id
[] = {
424 MODULE_DEVICE_TABLE(i2c
, sp2_id
);
426 static struct i2c_driver sp2_driver
= {
431 .remove
= sp2_remove
,
435 module_i2c_driver(sp2_driver
);
437 MODULE_DESCRIPTION("CIMaX SP2/HF CI driver");
438 MODULE_AUTHOR("Olli Salonen <olli.salonen@iki.fi>");
439 MODULE_LICENSE("GPL");