1 // SPDX-License-Identifier: GPL-2.0
3 * ddbridge-ci.c: Digital Devices bridge CI (DuoFlex, CI Bridge) support
5 * Copyright (C) 2010-2017 Digital Devices GmbH
6 * Marcus Metzler <mocm@metzlerbros.de>
7 * Ralph Metzler <rjkm@metzlerbros.de>
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * version 2 only, as published by the Free Software Foundation.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
20 #include "ddbridge-regs.h"
21 #include "ddbridge-ci.h"
22 #include "ddbridge-io.h"
23 #include "ddbridge-i2c.h"
27 /* Octopus CI internal CI interface */
29 static int wait_ci_ready(struct ddb_ci
*ci
)
35 if (ddbreadl(ci
->port
->dev
,
36 CI_CONTROL(ci
->nr
)) & CI_READY
)
45 static int read_attribute_mem(struct dvb_ca_en50221
*ca
,
46 int slot
, int address
)
48 struct ddb_ci
*ci
= ca
->data
;
49 u32 val
, off
= (address
>> 1) & (CI_BUFFER_SIZE
- 1);
51 if (address
> CI_BUFFER_SIZE
)
53 ddbwritel(ci
->port
->dev
, CI_READ_CMD
| (1 << 16) | address
,
54 CI_DO_READ_ATTRIBUTES(ci
->nr
));
56 val
= 0xff & ddbreadl(ci
->port
->dev
, CI_BUFFER(ci
->nr
) + off
);
60 static int write_attribute_mem(struct dvb_ca_en50221
*ca
, int slot
,
61 int address
, u8 value
)
63 struct ddb_ci
*ci
= ca
->data
;
65 ddbwritel(ci
->port
->dev
, CI_WRITE_CMD
| (value
<< 16) | address
,
66 CI_DO_ATTRIBUTE_RW(ci
->nr
));
71 static int read_cam_control(struct dvb_ca_en50221
*ca
,
75 struct ddb_ci
*ci
= ca
->data
;
78 ddbwritel(ci
->port
->dev
, CI_READ_CMD
| address
,
82 res
= ddbreadl(ci
->port
->dev
, CI_READDATA(ci
->nr
));
92 static int write_cam_control(struct dvb_ca_en50221
*ca
, int slot
,
95 struct ddb_ci
*ci
= ca
->data
;
97 ddbwritel(ci
->port
->dev
, CI_WRITE_CMD
| (value
<< 16) | address
,
103 static int slot_reset(struct dvb_ca_en50221
*ca
, int slot
)
105 struct ddb_ci
*ci
= ca
->data
;
107 ddbwritel(ci
->port
->dev
, CI_POWER_ON
,
110 ddbwritel(ci
->port
->dev
, CI_POWER_ON
| CI_RESET_CAM
,
112 ddbwritel(ci
->port
->dev
, CI_ENABLE
| CI_POWER_ON
| CI_RESET_CAM
,
114 usleep_range(20, 25);
115 ddbwritel(ci
->port
->dev
, CI_ENABLE
| CI_POWER_ON
,
120 static int slot_shutdown(struct dvb_ca_en50221
*ca
, int slot
)
122 struct ddb_ci
*ci
= ca
->data
;
124 ddbwritel(ci
->port
->dev
, 0, CI_CONTROL(ci
->nr
));
129 static int slot_ts_enable(struct dvb_ca_en50221
*ca
, int slot
)
131 struct ddb_ci
*ci
= ca
->data
;
132 u32 val
= ddbreadl(ci
->port
->dev
, CI_CONTROL(ci
->nr
));
134 ddbwritel(ci
->port
->dev
, val
| CI_BYPASS_DISABLE
,
139 static int poll_slot_status(struct dvb_ca_en50221
*ca
, int slot
, int open
)
141 struct ddb_ci
*ci
= ca
->data
;
142 u32 val
= ddbreadl(ci
->port
->dev
, CI_CONTROL(ci
->nr
));
145 if (val
& CI_CAM_DETECT
)
146 stat
|= DVB_CA_EN50221_POLL_CAM_PRESENT
;
147 if (val
& CI_CAM_READY
)
148 stat
|= DVB_CA_EN50221_POLL_CAM_READY
;
152 static struct dvb_ca_en50221 en_templ
= {
153 .read_attribute_mem
= read_attribute_mem
,
154 .write_attribute_mem
= write_attribute_mem
,
155 .read_cam_control
= read_cam_control
,
156 .write_cam_control
= write_cam_control
,
157 .slot_reset
= slot_reset
,
158 .slot_shutdown
= slot_shutdown
,
159 .slot_ts_enable
= slot_ts_enable
,
160 .poll_slot_status
= poll_slot_status
,
163 static void ci_attach(struct ddb_port
*port
)
167 ci
= kzalloc(sizeof(*ci
), GFP_KERNEL
);
170 memcpy(&ci
->en
, &en_templ
, sizeof(en_templ
));
173 port
->en_freedata
= 1;
175 ci
->nr
= port
->nr
- 2;
178 /* DuoFlex Dual CI support */
180 static int write_creg(struct ddb_ci
*ci
, u8 data
, u8 mask
)
182 struct i2c_adapter
*i2c
= &ci
->port
->i2c
->adap
;
183 u8 adr
= (ci
->port
->type
== DDB_CI_EXTERNAL_XO2
) ? 0x12 : 0x13;
185 ci
->port
->creg
= (ci
->port
->creg
& ~mask
) | data
;
186 return i2c_write_reg(i2c
, adr
, 0x02, ci
->port
->creg
);
189 static int read_attribute_mem_xo2(struct dvb_ca_en50221
*ca
,
190 int slot
, int address
)
192 struct ddb_ci
*ci
= ca
->data
;
193 struct i2c_adapter
*i2c
= &ci
->port
->i2c
->adap
;
194 u8 adr
= (ci
->port
->type
== DDB_CI_EXTERNAL_XO2
) ? 0x12 : 0x13;
198 res
= i2c_read_reg16(i2c
, adr
, 0x8000 | address
, &val
);
199 return res
? res
: val
;
202 static int write_attribute_mem_xo2(struct dvb_ca_en50221
*ca
, int slot
,
203 int address
, u8 value
)
205 struct ddb_ci
*ci
= ca
->data
;
206 struct i2c_adapter
*i2c
= &ci
->port
->i2c
->adap
;
207 u8 adr
= (ci
->port
->type
== DDB_CI_EXTERNAL_XO2
) ? 0x12 : 0x13;
209 return i2c_write_reg16(i2c
, adr
, 0x8000 | address
, value
);
212 static int read_cam_control_xo2(struct dvb_ca_en50221
*ca
,
213 int slot
, u8 address
)
215 struct ddb_ci
*ci
= ca
->data
;
216 struct i2c_adapter
*i2c
= &ci
->port
->i2c
->adap
;
217 u8 adr
= (ci
->port
->type
== DDB_CI_EXTERNAL_XO2
) ? 0x12 : 0x13;
221 res
= i2c_read_reg(i2c
, adr
, 0x20 | (address
& 3), &val
);
222 return res
? res
: val
;
225 static int write_cam_control_xo2(struct dvb_ca_en50221
*ca
, int slot
,
226 u8 address
, u8 value
)
228 struct ddb_ci
*ci
= ca
->data
;
229 struct i2c_adapter
*i2c
= &ci
->port
->i2c
->adap
;
230 u8 adr
= (ci
->port
->type
== DDB_CI_EXTERNAL_XO2
) ? 0x12 : 0x13;
232 return i2c_write_reg(i2c
, adr
, 0x20 | (address
& 3), value
);
235 static int slot_reset_xo2(struct dvb_ca_en50221
*ca
, int slot
)
237 struct ddb_ci
*ci
= ca
->data
;
239 dev_dbg(ci
->port
->dev
->dev
, "%s\n", __func__
);
240 write_creg(ci
, 0x01, 0x01);
241 write_creg(ci
, 0x04, 0x04);
243 write_creg(ci
, 0x02, 0x02);
244 write_creg(ci
, 0x00, 0x04);
245 write_creg(ci
, 0x18, 0x18);
249 static int slot_shutdown_xo2(struct dvb_ca_en50221
*ca
, int slot
)
251 struct ddb_ci
*ci
= ca
->data
;
253 dev_dbg(ci
->port
->dev
->dev
, "%s\n", __func__
);
254 write_creg(ci
, 0x10, 0xff);
255 write_creg(ci
, 0x08, 0x08);
259 static int slot_ts_enable_xo2(struct dvb_ca_en50221
*ca
, int slot
)
261 struct ddb_ci
*ci
= ca
->data
;
263 dev_dbg(ci
->port
->dev
->dev
, "%s\n", __func__
);
264 write_creg(ci
, 0x00, 0x10);
268 static int poll_slot_status_xo2(struct dvb_ca_en50221
*ca
, int slot
, int open
)
270 struct ddb_ci
*ci
= ca
->data
;
271 struct i2c_adapter
*i2c
= &ci
->port
->i2c
->adap
;
272 u8 adr
= (ci
->port
->type
== DDB_CI_EXTERNAL_XO2
) ? 0x12 : 0x13;
276 i2c_read_reg(i2c
, adr
, 0x01, &val
);
279 stat
|= DVB_CA_EN50221_POLL_CAM_PRESENT
;
281 stat
|= DVB_CA_EN50221_POLL_CAM_READY
;
285 static struct dvb_ca_en50221 en_xo2_templ
= {
286 .read_attribute_mem
= read_attribute_mem_xo2
,
287 .write_attribute_mem
= write_attribute_mem_xo2
,
288 .read_cam_control
= read_cam_control_xo2
,
289 .write_cam_control
= write_cam_control_xo2
,
290 .slot_reset
= slot_reset_xo2
,
291 .slot_shutdown
= slot_shutdown_xo2
,
292 .slot_ts_enable
= slot_ts_enable_xo2
,
293 .poll_slot_status
= poll_slot_status_xo2
,
296 static void ci_xo2_attach(struct ddb_port
*port
)
300 ci
= kzalloc(sizeof(*ci
), GFP_KERNEL
);
303 memcpy(&ci
->en
, &en_xo2_templ
, sizeof(en_xo2_templ
));
306 port
->en_freedata
= 1;
308 ci
->nr
= port
->nr
- 2;
310 write_creg(ci
, 0x10, 0xff);
311 write_creg(ci
, 0x08, 0x08);
314 static const struct cxd2099_cfg cxd_cfgtmpl
= {
321 static int ci_cxd2099_attach(struct ddb_port
*port
, u32 bitrate
)
323 struct cxd2099_cfg cxd_cfg
= cxd_cfgtmpl
;
324 struct i2c_client
*client
;
326 cxd_cfg
.bitrate
= bitrate
;
327 cxd_cfg
.en
= &port
->en
;
329 client
= dvb_module_probe("cxd2099", NULL
, &port
->i2c
->adap
,
334 port
->dvb
[0].i2c_client
[0] = client
;
335 port
->en_freedata
= 0;
339 dev_err(port
->dev
->dev
, "CXD2099AR attach failed\n");
343 int ddb_ci_attach(struct ddb_port
*port
, u32 bitrate
)
347 switch (port
->type
) {
348 case DDB_CI_EXTERNAL_SONY
:
349 ret
= ci_cxd2099_attach(port
, bitrate
);
353 case DDB_CI_EXTERNAL_XO2
:
354 case DDB_CI_EXTERNAL_XO2_B
:
357 case DDB_CI_INTERNAL
:
366 dvb_ca_en50221_init(port
->dvb
[0].adap
, port
->en
, 0, 1);
370 void ddb_ci_detach(struct ddb_port
*port
)
372 if (port
->dvb
[0].dev
)
373 dvb_unregister_device(port
->dvb
[0].dev
);
375 dvb_ca_en50221_release(port
->en
);
377 dvb_module_release(port
->dvb
[0].i2c_client
[0]);
378 port
->dvb
[0].i2c_client
[0] = NULL
;
380 /* free alloc'ed memory if needed */
381 if (port
->en_freedata
)
382 kfree(port
->en
->data
);