Linux 4.19.133
[linux/fpc-iii.git] / drivers / media / pci / ddbridge / ddbridge-ci.c
blobcfe23d02e5613d432ba52eab2ef273026b98e3c5
1 /*
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
21 #include "ddbridge.h"
22 #include "ddbridge-regs.h"
23 #include "ddbridge-ci.h"
24 #include "ddbridge-io.h"
25 #include "ddbridge-i2c.h"
27 #include "cxd2099.h"
29 /* Octopus CI internal CI interface */
31 static int wait_ci_ready(struct ddb_ci *ci)
33 u32 count = 10;
35 ndelay(500);
36 do {
37 if (ddbreadl(ci->port->dev,
38 CI_CONTROL(ci->nr)) & CI_READY)
39 break;
40 usleep_range(1, 2);
41 if ((--count) == 0)
42 return -1;
43 } while (1);
44 return 0;
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)
54 return -1;
55 ddbwritel(ci->port->dev, CI_READ_CMD | (1 << 16) | address,
56 CI_DO_READ_ATTRIBUTES(ci->nr));
57 wait_ci_ready(ci);
58 val = 0xff & ddbreadl(ci->port->dev, CI_BUFFER(ci->nr) + off);
59 return val;
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));
69 wait_ci_ready(ci);
70 return 0;
73 static int read_cam_control(struct dvb_ca_en50221 *ca,
74 int slot, u8 address)
76 u32 count = 100;
77 struct ddb_ci *ci = ca->data;
78 u32 res;
80 ddbwritel(ci->port->dev, CI_READ_CMD | address,
81 CI_DO_IO_RW(ci->nr));
82 ndelay(500);
83 do {
84 res = ddbreadl(ci->port->dev, CI_READDATA(ci->nr));
85 if (res & CI_READY)
86 break;
87 usleep_range(1, 2);
88 if ((--count) == 0)
89 return -1;
90 } while (1);
91 return 0xff & res;
94 static int write_cam_control(struct dvb_ca_en50221 *ca, int slot,
95 u8 address, u8 value)
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));
101 wait_ci_ready(ci);
102 return 0;
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,
110 CI_CONTROL(ci->nr));
111 msleep(100);
112 ddbwritel(ci->port->dev, CI_POWER_ON | CI_RESET_CAM,
113 CI_CONTROL(ci->nr));
114 ddbwritel(ci->port->dev, CI_ENABLE | CI_POWER_ON | CI_RESET_CAM,
115 CI_CONTROL(ci->nr));
116 usleep_range(20, 25);
117 ddbwritel(ci->port->dev, CI_ENABLE | CI_POWER_ON,
118 CI_CONTROL(ci->nr));
119 return 0;
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));
127 msleep(300);
128 return 0;
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,
137 CI_CONTROL(ci->nr));
138 return 0;
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));
145 int stat = 0;
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;
151 return stat;
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)
167 struct ddb_ci *ci;
169 ci = kzalloc(sizeof(*ci), GFP_KERNEL);
170 if (!ci)
171 return;
172 memcpy(&ci->en, &en_templ, sizeof(en_templ));
173 ci->en.data = ci;
174 port->en = &ci->en;
175 port->en_freedata = 1;
176 ci->port = port;
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;
197 int res;
198 u8 val;
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;
220 u8 val;
221 int res;
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);
244 msleep(20);
245 write_creg(ci, 0x02, 0x02);
246 write_creg(ci, 0x00, 0x04);
247 write_creg(ci, 0x18, 0x18);
248 return 0;
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);
258 return 0;
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);
267 return 0;
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;
275 u8 val = 0;
276 int stat = 0;
278 i2c_read_reg(i2c, adr, 0x01, &val);
280 if (val & 2)
281 stat |= DVB_CA_EN50221_POLL_CAM_PRESENT;
282 if (val & 1)
283 stat |= DVB_CA_EN50221_POLL_CAM_READY;
284 return stat;
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)
300 struct ddb_ci *ci;
302 ci = kzalloc(sizeof(*ci), GFP_KERNEL);
303 if (!ci)
304 return;
305 memcpy(&ci->en, &en_xo2_templ, sizeof(en_xo2_templ));
306 ci->en.data = ci;
307 port->en = &ci->en;
308 port->en_freedata = 1;
309 ci->port = port;
310 ci->nr = port->nr - 2;
311 ci->port->creg = 0;
312 write_creg(ci, 0x10, 0xff);
313 write_creg(ci, 0x08, 0x08);
316 static const struct cxd2099_cfg cxd_cfgtmpl = {
317 .bitrate = 72000,
318 .polarity = 1,
319 .clock_mode = 1,
320 .max_i2c = 512,
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,
332 0x40, &cxd_cfg);
333 if (!client)
334 goto err;
336 port->dvb[0].i2c_client[0] = client;
337 port->en_freedata = 0;
338 return 0;
340 err:
341 dev_err(port->dev->dev, "CXD2099AR attach failed\n");
342 return -ENODEV;
345 int ddb_ci_attach(struct ddb_port *port, u32 bitrate)
347 int ret;
349 switch (port->type) {
350 case DDB_CI_EXTERNAL_SONY:
351 ret = ci_cxd2099_attach(port, bitrate);
352 if (ret)
353 return -ENODEV;
354 break;
355 case DDB_CI_EXTERNAL_XO2:
356 case DDB_CI_EXTERNAL_XO2_B:
357 ci_xo2_attach(port);
358 break;
359 case DDB_CI_INTERNAL:
360 ci_attach(port);
361 break;
362 default:
363 return -ENODEV;
366 if (!port->en)
367 return -ENODEV;
368 dvb_ca_en50221_init(port->dvb[0].adap, port->en, 0, 1);
369 return 0;
372 void ddb_ci_detach(struct ddb_port *port)
374 if (port->dvb[0].dev)
375 dvb_unregister_device(port->dvb[0].dev);
376 if (port->en) {
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);
386 port->en = NULL;