1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * CIMaX SP2/SP2HF (Atmel T90FJR) CI driver
5 * Copyright (C) 2014 Olli Salonen <olli.salonen@iki.fi>
7 * Heavily based on CIMax2(R) SP2 driver in conjunction with NetUp Dual
8 * DVB-S2 CI card (cimax2) with following copyrights:
10 * Copyright (C) 2009 NetUP Inc.
11 * Copyright (C) 2009 Igor M. Liplianin <liplianin@netup.ru>
12 * Copyright (C) 2009 Abylay Ospan <aospan@netup.ru>
17 static int sp2_read_i2c(struct sp2
*s
, u8 reg
, u8
*buf
, int len
)
20 struct i2c_client
*client
= s
->client
;
21 struct i2c_adapter
*adap
= client
->adapter
;
22 struct i2c_msg msg
[] = {
36 ret
= i2c_transfer(adap
, msg
, 2);
39 dev_err(&client
->dev
, "i2c read error, reg = 0x%02x, status = %d\n",
47 dev_dbg(&s
->client
->dev
, "addr=0x%04x, reg = 0x%02x, data = %02x\n",
48 client
->addr
, reg
, buf
[0]);
53 static int sp2_write_i2c(struct sp2
*s
, u8 reg
, u8
*buf
, int len
)
57 struct i2c_client
*client
= s
->client
;
58 struct i2c_adapter
*adap
= client
->adapter
;
59 struct i2c_msg msg
= {
66 if ((len
+ 1) > sizeof(buffer
)) {
67 dev_err(&client
->dev
, "i2c wr reg=%02x: len=%d is too big!\n",
73 memcpy(&buffer
[1], buf
, len
);
75 ret
= i2c_transfer(adap
, &msg
, 1);
78 dev_err(&client
->dev
, "i2c write error, reg = 0x%02x, status = %d\n",
86 dev_dbg(&s
->client
->dev
, "addr=0x%04x, reg = 0x%02x, data = %*ph\n",
87 client
->addr
, reg
, len
, buf
);
92 static int sp2_ci_op_cam(struct dvb_ca_en50221
*en50221
, int slot
, u8 acs
,
93 u8 read
, int addr
, u8 data
)
95 struct sp2
*s
= en50221
->data
;
98 int (*ci_op_cam
)(void*, u8
, int, u8
, int*) = s
->ci_control
;
104 * change module access type between IO space and attribute memory
107 if (s
->module_access_type
!= acs
) {
108 ret
= sp2_read_i2c(s
, 0x00, &store
, 1);
113 store
&= ~(SP2_MOD_CTL_ACS1
| SP2_MOD_CTL_ACS0
);
116 ret
= sp2_write_i2c(s
, 0x00, &store
, 1);
121 s
->module_access_type
= acs
;
123 /* implementation of ci_op_cam is device specific */
125 ret
= ci_op_cam(s
->priv
, read
, addr
, data
, &mem
);
127 dev_err(&s
->client
->dev
, "callback not defined");
134 dev_dbg(&s
->client
->dev
, "%s: slot=%d, addr=0x%04x, %s, data=%x",
135 (read
) ? "read" : "write", slot
, addr
,
136 (acs
== SP2_CI_ATTR_ACS
) ? "attr" : "io",
137 (read
) ? mem
: data
);
146 int sp2_ci_read_attribute_mem(struct dvb_ca_en50221
*en50221
,
149 return sp2_ci_op_cam(en50221
, slot
, SP2_CI_ATTR_ACS
,
153 int sp2_ci_write_attribute_mem(struct dvb_ca_en50221
*en50221
,
154 int slot
, int addr
, u8 data
)
156 return sp2_ci_op_cam(en50221
, slot
, SP2_CI_ATTR_ACS
,
157 SP2_CI_WR
, addr
, data
);
160 int sp2_ci_read_cam_control(struct dvb_ca_en50221
*en50221
,
163 return sp2_ci_op_cam(en50221
, slot
, SP2_CI_IO_ACS
,
167 int sp2_ci_write_cam_control(struct dvb_ca_en50221
*en50221
,
168 int slot
, u8 addr
, u8 data
)
170 return sp2_ci_op_cam(en50221
, slot
, SP2_CI_IO_ACS
,
171 SP2_CI_WR
, addr
, data
);
174 int sp2_ci_slot_reset(struct dvb_ca_en50221
*en50221
, int slot
)
176 struct sp2
*s
= en50221
->data
;
180 dev_dbg(&s
->client
->dev
, "slot: %d\n", slot
);
186 buf
= SP2_MOD_CTL_RST
;
187 ret
= sp2_write_i2c(s
, 0x00, &buf
, 1);
192 usleep_range(500, 600);
196 ret
= sp2_write_i2c(s
, 0x00, &buf
, 1);
206 int sp2_ci_slot_shutdown(struct dvb_ca_en50221
*en50221
, int slot
)
208 struct sp2
*s
= en50221
->data
;
210 dev_dbg(&s
->client
->dev
, "slot:%d\n", slot
);
212 /* not implemented */
216 int sp2_ci_slot_ts_enable(struct dvb_ca_en50221
*en50221
, int slot
)
218 struct sp2
*s
= en50221
->data
;
221 dev_dbg(&s
->client
->dev
, "slot:%d\n", slot
);
226 sp2_read_i2c(s
, 0x00, &buf
, 1);
228 /* disable bypass and enable TS */
229 buf
|= (SP2_MOD_CTL_TSOEN
| SP2_MOD_CTL_TSIEN
);
230 return sp2_write_i2c(s
, 0, &buf
, 1);
233 int sp2_ci_poll_slot_status(struct dvb_ca_en50221
*en50221
,
236 struct sp2
*s
= en50221
->data
;
240 dev_dbg(&s
->client
->dev
, "slot:%d open:%d\n", slot
, open
);
243 * CAM module INSERT/REMOVE processing. Slow operation because of i2c
244 * transfers. Throttle read to one per sec.
246 if (time_after(jiffies
, s
->next_status_checked_time
)) {
247 ret
= sp2_read_i2c(s
, 0x00, buf
, 1);
248 s
->next_status_checked_time
= jiffies
+ msecs_to_jiffies(1000);
253 if (buf
[0] & SP2_MOD_CTL_DET
)
254 s
->status
= DVB_CA_EN50221_POLL_CAM_PRESENT
|
255 DVB_CA_EN50221_POLL_CAM_READY
;
263 static int sp2_init(struct sp2
*s
)
267 u8 cimax_init
[34] = {
268 0x00, /* module A control*/
269 0x00, /* auto select mask high A */
270 0x00, /* auto select mask low A */
271 0x00, /* auto select pattern high A */
272 0x00, /* auto select pattern low A */
273 0x44, /* memory access time A, 600 ns */
274 0x00, /* invert input A */
277 0x00, /* module B control*/
278 0x00, /* auto select mask high B */
279 0x00, /* auto select mask low B */
280 0x00, /* auto select pattern high B */
281 0x00, /* auto select pattern low B */
282 0x44, /* memory access time B, 600 ns */
283 0x00, /* invert input B */
286 0x00, /* auto select mask high Ext */
287 0x00, /* auto select mask low Ext */
288 0x00, /* auto select pattern high Ext */
289 0x00, /* auto select pattern low Ext */
291 0x02, /* destination - module A */
292 0x01, /* power control reg, VCC power on */
294 0x00, /* int status read only */
295 0x00, /* Interrupt Mask Register */
296 0x05, /* EXTINT=active-high, INT=push-pull */
298 0x04, /* ack active low */
301 0x00, /* synchronization? */
304 dev_dbg(&s
->client
->dev
, "\n");
306 s
->ca
.owner
= THIS_MODULE
;
307 s
->ca
.read_attribute_mem
= sp2_ci_read_attribute_mem
;
308 s
->ca
.write_attribute_mem
= sp2_ci_write_attribute_mem
;
309 s
->ca
.read_cam_control
= sp2_ci_read_cam_control
;
310 s
->ca
.write_cam_control
= sp2_ci_write_cam_control
;
311 s
->ca
.slot_reset
= sp2_ci_slot_reset
;
312 s
->ca
.slot_shutdown
= sp2_ci_slot_shutdown
;
313 s
->ca
.slot_ts_enable
= sp2_ci_slot_ts_enable
;
314 s
->ca
.poll_slot_status
= sp2_ci_poll_slot_status
;
316 s
->module_access_type
= 0;
318 /* initialize all regs */
319 ret
= sp2_write_i2c(s
, 0x00, &cimax_init
[0], 34);
325 ret
= sp2_write_i2c(s
, 0x1f, &buf
, 1);
330 ret
= sp2_write_i2c(s
, 0x18, &buf
, 1);
334 ret
= dvb_ca_en50221_init(s
->dvb_adap
, &s
->ca
, 0, 1);
341 dev_dbg(&s
->client
->dev
, "init failed=%d\n", ret
);
345 static int sp2_exit(struct i2c_client
*client
)
349 dev_dbg(&client
->dev
, "\n");
354 s
= i2c_get_clientdata(client
);
361 dvb_ca_en50221_release(&s
->ca
);
366 static int sp2_probe(struct i2c_client
*client
,
367 const struct i2c_device_id
*id
)
369 struct sp2_config
*cfg
= client
->dev
.platform_data
;
373 dev_dbg(&client
->dev
, "\n");
375 s
= kzalloc(sizeof(*s
), GFP_KERNEL
);
382 s
->dvb_adap
= cfg
->dvb_adap
;
384 s
->ci_control
= cfg
->ci_control
;
386 i2c_set_clientdata(client
, s
);
392 dev_info(&s
->client
->dev
, "CIMaX SP2 successfully attached\n");
395 dev_dbg(&client
->dev
, "init failed=%d\n", ret
);
401 static int sp2_remove(struct i2c_client
*client
)
403 struct sp2
*s
= i2c_get_clientdata(client
);
405 dev_dbg(&client
->dev
, "\n");
411 static const struct i2c_device_id sp2_id
[] = {
415 MODULE_DEVICE_TABLE(i2c
, sp2_id
);
417 static struct i2c_driver sp2_driver
= {
422 .remove
= sp2_remove
,
426 module_i2c_driver(sp2_driver
);
428 MODULE_DESCRIPTION("CIMaX SP2/HF CI driver");
429 MODULE_AUTHOR("Olli Salonen <olli.salonen@iki.fi>");
430 MODULE_LICENSE("GPL");