2 * ddbridge-ci.c: Digital Devices bridge CI (DuoFlex, CI Bridge) support
4 * Copyright (C) 2010-2017 Digital Devices GmbH
5 * Marcus Metzler <mocm@metzlerbros.de>
6 * Ralph Metzler <rjkm@metzlerbros.de>
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * 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.
17 * To obtain the license, point your browser to
18 * http://www.gnu.org/copyleft/gpl.html
22 #include "ddbridge-regs.h"
23 #include "ddbridge-ci.h"
24 #include "ddbridge-io.h"
25 #include "ddbridge-i2c.h"
29 /* Octopus CI internal CI interface */
31 static int wait_ci_ready(struct ddb_ci
*ci
)
37 if (ddbreadl(ci
->port
->dev
,
38 CI_CONTROL(ci
->nr
)) & CI_READY
)
47 static int read_attribute_mem(struct dvb_ca_en50221
*ca
,
48 int slot
, int address
)
50 struct ddb_ci
*ci
= ca
->data
;
51 u32 val
, off
= (address
>> 1) & (CI_BUFFER_SIZE
- 1);
53 if (address
> CI_BUFFER_SIZE
)
55 ddbwritel(ci
->port
->dev
, CI_READ_CMD
| (1 << 16) | address
,
56 CI_DO_READ_ATTRIBUTES(ci
->nr
));
58 val
= 0xff & ddbreadl(ci
->port
->dev
, CI_BUFFER(ci
->nr
) + off
);
62 static int write_attribute_mem(struct dvb_ca_en50221
*ca
, int slot
,
63 int address
, u8 value
)
65 struct ddb_ci
*ci
= ca
->data
;
67 ddbwritel(ci
->port
->dev
, CI_WRITE_CMD
| (value
<< 16) | address
,
68 CI_DO_ATTRIBUTE_RW(ci
->nr
));
73 static int read_cam_control(struct dvb_ca_en50221
*ca
,
77 struct ddb_ci
*ci
= ca
->data
;
80 ddbwritel(ci
->port
->dev
, CI_READ_CMD
| address
,
84 res
= ddbreadl(ci
->port
->dev
, CI_READDATA(ci
->nr
));
94 static int write_cam_control(struct dvb_ca_en50221
*ca
, int slot
,
97 struct ddb_ci
*ci
= ca
->data
;
99 ddbwritel(ci
->port
->dev
, CI_WRITE_CMD
| (value
<< 16) | address
,
100 CI_DO_IO_RW(ci
->nr
));
105 static int slot_reset(struct dvb_ca_en50221
*ca
, int slot
)
107 struct ddb_ci
*ci
= ca
->data
;
109 ddbwritel(ci
->port
->dev
, CI_POWER_ON
,
112 ddbwritel(ci
->port
->dev
, CI_POWER_ON
| CI_RESET_CAM
,
114 ddbwritel(ci
->port
->dev
, CI_ENABLE
| CI_POWER_ON
| CI_RESET_CAM
,
116 usleep_range(20, 25);
117 ddbwritel(ci
->port
->dev
, CI_ENABLE
| CI_POWER_ON
,
122 static int slot_shutdown(struct dvb_ca_en50221
*ca
, int slot
)
124 struct ddb_ci
*ci
= ca
->data
;
126 ddbwritel(ci
->port
->dev
, 0, CI_CONTROL(ci
->nr
));
131 static int slot_ts_enable(struct dvb_ca_en50221
*ca
, int slot
)
133 struct ddb_ci
*ci
= ca
->data
;
134 u32 val
= ddbreadl(ci
->port
->dev
, CI_CONTROL(ci
->nr
));
136 ddbwritel(ci
->port
->dev
, val
| CI_BYPASS_DISABLE
,
141 static int poll_slot_status(struct dvb_ca_en50221
*ca
, int slot
, int open
)
143 struct ddb_ci
*ci
= ca
->data
;
144 u32 val
= ddbreadl(ci
->port
->dev
, CI_CONTROL(ci
->nr
));
147 if (val
& CI_CAM_DETECT
)
148 stat
|= DVB_CA_EN50221_POLL_CAM_PRESENT
;
149 if (val
& CI_CAM_READY
)
150 stat
|= DVB_CA_EN50221_POLL_CAM_READY
;
154 static struct dvb_ca_en50221 en_templ
= {
155 .read_attribute_mem
= read_attribute_mem
,
156 .write_attribute_mem
= write_attribute_mem
,
157 .read_cam_control
= read_cam_control
,
158 .write_cam_control
= write_cam_control
,
159 .slot_reset
= slot_reset
,
160 .slot_shutdown
= slot_shutdown
,
161 .slot_ts_enable
= slot_ts_enable
,
162 .poll_slot_status
= poll_slot_status
,
165 static void ci_attach(struct ddb_port
*port
)
169 ci
= kzalloc(sizeof(*ci
), GFP_KERNEL
);
172 memcpy(&ci
->en
, &en_templ
, sizeof(en_templ
));
175 port
->en_freedata
= 1;
177 ci
->nr
= port
->nr
- 2;
180 /* DuoFlex Dual CI support */
182 static int write_creg(struct ddb_ci
*ci
, u8 data
, u8 mask
)
184 struct i2c_adapter
*i2c
= &ci
->port
->i2c
->adap
;
185 u8 adr
= (ci
->port
->type
== DDB_CI_EXTERNAL_XO2
) ? 0x12 : 0x13;
187 ci
->port
->creg
= (ci
->port
->creg
& ~mask
) | data
;
188 return i2c_write_reg(i2c
, adr
, 0x02, ci
->port
->creg
);
191 static int read_attribute_mem_xo2(struct dvb_ca_en50221
*ca
,
192 int slot
, int address
)
194 struct ddb_ci
*ci
= ca
->data
;
195 struct i2c_adapter
*i2c
= &ci
->port
->i2c
->adap
;
196 u8 adr
= (ci
->port
->type
== DDB_CI_EXTERNAL_XO2
) ? 0x12 : 0x13;
200 res
= i2c_read_reg16(i2c
, adr
, 0x8000 | address
, &val
);
201 return res
? res
: val
;
204 static int write_attribute_mem_xo2(struct dvb_ca_en50221
*ca
, int slot
,
205 int address
, u8 value
)
207 struct ddb_ci
*ci
= ca
->data
;
208 struct i2c_adapter
*i2c
= &ci
->port
->i2c
->adap
;
209 u8 adr
= (ci
->port
->type
== DDB_CI_EXTERNAL_XO2
) ? 0x12 : 0x13;
211 return i2c_write_reg16(i2c
, adr
, 0x8000 | address
, value
);
214 static int read_cam_control_xo2(struct dvb_ca_en50221
*ca
,
215 int slot
, u8 address
)
217 struct ddb_ci
*ci
= ca
->data
;
218 struct i2c_adapter
*i2c
= &ci
->port
->i2c
->adap
;
219 u8 adr
= (ci
->port
->type
== DDB_CI_EXTERNAL_XO2
) ? 0x12 : 0x13;
223 res
= i2c_read_reg(i2c
, adr
, 0x20 | (address
& 3), &val
);
224 return res
? res
: val
;
227 static int write_cam_control_xo2(struct dvb_ca_en50221
*ca
, int slot
,
228 u8 address
, u8 value
)
230 struct ddb_ci
*ci
= ca
->data
;
231 struct i2c_adapter
*i2c
= &ci
->port
->i2c
->adap
;
232 u8 adr
= (ci
->port
->type
== DDB_CI_EXTERNAL_XO2
) ? 0x12 : 0x13;
234 return i2c_write_reg(i2c
, adr
, 0x20 | (address
& 3), value
);
237 static int slot_reset_xo2(struct dvb_ca_en50221
*ca
, int slot
)
239 struct ddb_ci
*ci
= ca
->data
;
241 dev_dbg(ci
->port
->dev
->dev
, "%s\n", __func__
);
242 write_creg(ci
, 0x01, 0x01);
243 write_creg(ci
, 0x04, 0x04);
245 write_creg(ci
, 0x02, 0x02);
246 write_creg(ci
, 0x00, 0x04);
247 write_creg(ci
, 0x18, 0x18);
251 static int slot_shutdown_xo2(struct dvb_ca_en50221
*ca
, int slot
)
253 struct ddb_ci
*ci
= ca
->data
;
255 dev_dbg(ci
->port
->dev
->dev
, "%s\n", __func__
);
256 write_creg(ci
, 0x10, 0xff);
257 write_creg(ci
, 0x08, 0x08);
261 static int slot_ts_enable_xo2(struct dvb_ca_en50221
*ca
, int slot
)
263 struct ddb_ci
*ci
= ca
->data
;
265 dev_dbg(ci
->port
->dev
->dev
, "%s\n", __func__
);
266 write_creg(ci
, 0x00, 0x10);
270 static int poll_slot_status_xo2(struct dvb_ca_en50221
*ca
, int slot
, int open
)
272 struct ddb_ci
*ci
= ca
->data
;
273 struct i2c_adapter
*i2c
= &ci
->port
->i2c
->adap
;
274 u8 adr
= (ci
->port
->type
== DDB_CI_EXTERNAL_XO2
) ? 0x12 : 0x13;
278 i2c_read_reg(i2c
, adr
, 0x01, &val
);
281 stat
|= DVB_CA_EN50221_POLL_CAM_PRESENT
;
283 stat
|= DVB_CA_EN50221_POLL_CAM_READY
;
287 static struct dvb_ca_en50221 en_xo2_templ
= {
288 .read_attribute_mem
= read_attribute_mem_xo2
,
289 .write_attribute_mem
= write_attribute_mem_xo2
,
290 .read_cam_control
= read_cam_control_xo2
,
291 .write_cam_control
= write_cam_control_xo2
,
292 .slot_reset
= slot_reset_xo2
,
293 .slot_shutdown
= slot_shutdown_xo2
,
294 .slot_ts_enable
= slot_ts_enable_xo2
,
295 .poll_slot_status
= poll_slot_status_xo2
,
298 static void ci_xo2_attach(struct ddb_port
*port
)
302 ci
= kzalloc(sizeof(*ci
), GFP_KERNEL
);
305 memcpy(&ci
->en
, &en_xo2_templ
, sizeof(en_xo2_templ
));
308 port
->en_freedata
= 1;
310 ci
->nr
= port
->nr
- 2;
312 write_creg(ci
, 0x10, 0xff);
313 write_creg(ci
, 0x08, 0x08);
316 static const struct cxd2099_cfg cxd_cfgtmpl
= {
323 static int ci_cxd2099_attach(struct ddb_port
*port
, u32 bitrate
)
325 struct cxd2099_cfg cxd_cfg
= cxd_cfgtmpl
;
326 struct i2c_client
*client
;
328 cxd_cfg
.bitrate
= bitrate
;
329 cxd_cfg
.en
= &port
->en
;
331 client
= dvb_module_probe("cxd2099", NULL
, &port
->i2c
->adap
,
336 port
->dvb
[0].i2c_client
[0] = client
;
337 port
->en_freedata
= 0;
341 dev_err(port
->dev
->dev
, "CXD2099AR attach failed\n");
345 int ddb_ci_attach(struct ddb_port
*port
, u32 bitrate
)
349 switch (port
->type
) {
350 case DDB_CI_EXTERNAL_SONY
:
351 ret
= ci_cxd2099_attach(port
, bitrate
);
355 case DDB_CI_EXTERNAL_XO2
:
356 case DDB_CI_EXTERNAL_XO2_B
:
359 case DDB_CI_INTERNAL
:
368 dvb_ca_en50221_init(port
->dvb
[0].adap
, port
->en
, 0, 1);
372 void ddb_ci_detach(struct ddb_port
*port
)
374 if (port
->dvb
[0].dev
)
375 dvb_unregister_device(port
->dvb
[0].dev
);
377 dvb_ca_en50221_release(port
->en
);
379 dvb_module_release(port
->dvb
[0].i2c_client
[0]);
380 port
->dvb
[0].i2c_client
[0] = NULL
;
382 /* free alloc'ed memory if needed */
383 if (port
->en_freedata
)
384 kfree(port
->en
->data
);