1 // SPDX-License-Identifier: GPL-2.0
3 * cxd2099.c: Driver for the Sony CXD2099AR Common Interface Controller
5 * Copyright (C) 2010-2013 Digital Devices GmbH
8 #include <linux/slab.h>
9 #include <linux/kernel.h>
10 #include <linux/module.h>
11 #include <linux/i2c.h>
12 #include <linux/regmap.h>
13 #include <linux/wait.h>
14 #include <linux/delay.h>
15 #include <linux/mutex.h>
20 static int buffermode
;
21 module_param(buffermode
, int, 0444);
22 MODULE_PARM_DESC(buffermode
, "Enable CXD2099AR buffer mode (default: disabled)");
24 static int read_data(struct dvb_ca_en50221
*ca
, int slot
, u8
*ebuf
, int ecount
);
27 struct dvb_ca_en50221 en
;
29 struct cxd2099_cfg cfg
;
30 struct i2c_client
*client
;
31 struct regmap
*regmap
;
47 struct mutex lock
; /* device access lock */
53 static int read_block(struct cxd
*ci
, u8 adr
, u8
*data
, u16 n
)
57 if (ci
->lastaddress
!= adr
)
58 status
= regmap_write(ci
->regmap
, 0, adr
);
60 ci
->lastaddress
= adr
;
65 if (ci
->cfg
.max_i2c
&& len
> ci
->cfg
.max_i2c
)
66 len
= ci
->cfg
.max_i2c
;
67 status
= regmap_raw_read(ci
->regmap
, 1, data
, len
);
77 static int read_reg(struct cxd
*ci
, u8 reg
, u8
*val
)
79 return read_block(ci
, reg
, val
, 1);
82 static int read_pccard(struct cxd
*ci
, u16 address
, u8
*data
, u8 n
)
85 u8 addr
[2] = {address
& 0xff, address
>> 8};
87 status
= regmap_raw_write(ci
->regmap
, 2, addr
, 2);
89 status
= regmap_raw_read(ci
->regmap
, 3, data
, n
);
93 static int write_pccard(struct cxd
*ci
, u16 address
, u8
*data
, u8 n
)
96 u8 addr
[2] = {address
& 0xff, address
>> 8};
98 status
= regmap_raw_write(ci
->regmap
, 2, addr
, 2);
102 memcpy(buf
, data
, n
);
103 status
= regmap_raw_write(ci
->regmap
, 3, buf
, n
);
108 static int read_io(struct cxd
*ci
, u16 address
, unsigned int *val
)
111 u8 addr
[2] = {address
& 0xff, address
>> 8};
113 status
= regmap_raw_write(ci
->regmap
, 2, addr
, 2);
115 status
= regmap_read(ci
->regmap
, 3, val
);
119 static int write_io(struct cxd
*ci
, u16 address
, u8 val
)
122 u8 addr
[2] = {address
& 0xff, address
>> 8};
124 status
= regmap_raw_write(ci
->regmap
, 2, addr
, 2);
126 status
= regmap_write(ci
->regmap
, 3, val
);
130 static int write_regm(struct cxd
*ci
, u8 reg
, u8 val
, u8 mask
)
135 if (ci
->lastaddress
!= reg
)
136 status
= regmap_write(ci
->regmap
, 0, reg
);
137 if (!status
&& reg
>= 6 && reg
<= 8 && mask
!= 0xff) {
138 status
= regmap_read(ci
->regmap
, 1, ®val
);
139 ci
->regs
[reg
] = regval
;
141 ci
->lastaddress
= reg
;
142 ci
->regs
[reg
] = (ci
->regs
[reg
] & (~mask
)) | val
;
144 status
= regmap_write(ci
->regmap
, 1, ci
->regs
[reg
]);
146 ci
->regs
[reg
] &= 0x7f;
150 static int write_reg(struct cxd
*ci
, u8 reg
, u8 val
)
152 return write_regm(ci
, reg
, val
, 0xff);
155 static int write_block(struct cxd
*ci
, u8 adr
, u8
*data
, u16 n
)
160 if (ci
->lastaddress
!= adr
)
161 status
= regmap_write(ci
->regmap
, 0, adr
);
165 ci
->lastaddress
= adr
;
169 if (ci
->cfg
.max_i2c
&& (len
+ 1 > ci
->cfg
.max_i2c
))
170 len
= ci
->cfg
.max_i2c
- 1;
171 memcpy(buf
, data
, len
);
172 status
= regmap_raw_write(ci
->regmap
, 1, buf
, len
);
181 static void set_mode(struct cxd
*ci
, int mode
)
183 if (mode
== ci
->mode
)
187 case 0x00: /* IO mem */
188 write_regm(ci
, 0x06, 0x00, 0x07);
190 case 0x01: /* ATT mem */
191 write_regm(ci
, 0x06, 0x02, 0x07);
199 static void cam_mode(struct cxd
*ci
, int mode
)
203 if (mode
== ci
->cammode
)
208 write_regm(ci
, 0x20, 0x80, 0x80);
211 if (!ci
->en
.read_data
)
214 dev_info(&ci
->client
->dev
, "enable cam buffer mode\n");
215 write_reg(ci
, 0x0d, 0x00);
216 write_reg(ci
, 0x0e, 0x01);
217 write_regm(ci
, 0x08, 0x40, 0x40);
218 read_reg(ci
, 0x12, &dummy
);
219 write_regm(ci
, 0x08, 0x80, 0x80);
227 static int init(struct cxd
*ci
)
231 mutex_lock(&ci
->lock
);
234 status
= write_reg(ci
, 0x00, 0x00);
237 status
= write_reg(ci
, 0x01, 0x00);
240 status
= write_reg(ci
, 0x02, 0x10);
243 status
= write_reg(ci
, 0x03, 0x00);
246 status
= write_reg(ci
, 0x05, 0xFF);
249 status
= write_reg(ci
, 0x06, 0x1F);
252 status
= write_reg(ci
, 0x07, 0x1F);
255 status
= write_reg(ci
, 0x08, 0x28);
258 status
= write_reg(ci
, 0x14, 0x20);
262 /* TOSTRT = 8, Mode B (gated clock), falling Edge,
263 * Serial, POL=HIGH, MSB
265 status
= write_reg(ci
, 0x0A, 0xA7);
269 status
= write_reg(ci
, 0x0B, 0x33);
272 status
= write_reg(ci
, 0x0C, 0x33);
276 status
= write_regm(ci
, 0x14, 0x00, 0x0F);
279 status
= write_reg(ci
, 0x15, ci
->clk_reg_b
);
282 status
= write_regm(ci
, 0x16, 0x00, 0x0F);
285 status
= write_reg(ci
, 0x17, ci
->clk_reg_f
);
289 if (ci
->cfg
.clock_mode
== 2) {
290 /* bitrate*2^13/ 72000 */
291 u32 reg
= ((ci
->cfg
.bitrate
<< 13) + 71999) / 72000;
293 if (ci
->cfg
.polarity
) {
294 status
= write_reg(ci
, 0x09, 0x6f);
298 status
= write_reg(ci
, 0x09, 0x6d);
302 status
= write_reg(ci
, 0x20, 0x08);
305 status
= write_reg(ci
, 0x21, (reg
>> 8) & 0xff);
308 status
= write_reg(ci
, 0x22, reg
& 0xff);
311 } else if (ci
->cfg
.clock_mode
== 1) {
312 if (ci
->cfg
.polarity
) {
313 status
= write_reg(ci
, 0x09, 0x6f); /* D */
317 status
= write_reg(ci
, 0x09, 0x6d);
321 status
= write_reg(ci
, 0x20, 0x68);
324 status
= write_reg(ci
, 0x21, 0x00);
327 status
= write_reg(ci
, 0x22, 0x02);
331 if (ci
->cfg
.polarity
) {
332 status
= write_reg(ci
, 0x09, 0x4f); /* C */
336 status
= write_reg(ci
, 0x09, 0x4d);
340 status
= write_reg(ci
, 0x20, 0x28);
343 status
= write_reg(ci
, 0x21, 0x00);
346 status
= write_reg(ci
, 0x22, 0x07);
351 status
= write_regm(ci
, 0x20, 0x80, 0x80);
354 status
= write_regm(ci
, 0x03, 0x02, 0x02);
357 status
= write_reg(ci
, 0x01, 0x04);
360 status
= write_reg(ci
, 0x00, 0x31);
364 /* Put TS in bypass */
365 status
= write_regm(ci
, 0x09, 0x08, 0x08);
371 mutex_unlock(&ci
->lock
);
376 static int read_attribute_mem(struct dvb_ca_en50221
*ca
,
377 int slot
, int address
)
379 struct cxd
*ci
= ca
->data
;
382 mutex_lock(&ci
->lock
);
384 read_pccard(ci
, address
, &val
, 1);
385 mutex_unlock(&ci
->lock
);
389 static int write_attribute_mem(struct dvb_ca_en50221
*ca
, int slot
,
390 int address
, u8 value
)
392 struct cxd
*ci
= ca
->data
;
394 mutex_lock(&ci
->lock
);
396 write_pccard(ci
, address
, &value
, 1);
397 mutex_unlock(&ci
->lock
);
401 static int read_cam_control(struct dvb_ca_en50221
*ca
,
402 int slot
, u8 address
)
404 struct cxd
*ci
= ca
->data
;
407 mutex_lock(&ci
->lock
);
409 read_io(ci
, address
, &val
);
410 mutex_unlock(&ci
->lock
);
414 static int write_cam_control(struct dvb_ca_en50221
*ca
, int slot
,
415 u8 address
, u8 value
)
417 struct cxd
*ci
= ca
->data
;
419 mutex_lock(&ci
->lock
);
421 write_io(ci
, address
, value
);
422 mutex_unlock(&ci
->lock
);
426 static int slot_reset(struct dvb_ca_en50221
*ca
, int slot
)
428 struct cxd
*ci
= ca
->data
;
431 read_data(ca
, slot
, ci
->rbuf
, 0);
433 mutex_lock(&ci
->lock
);
435 write_reg(ci
, 0x00, 0x21);
436 write_reg(ci
, 0x06, 0x1F);
437 write_reg(ci
, 0x00, 0x31);
438 write_regm(ci
, 0x20, 0x80, 0x80);
439 write_reg(ci
, 0x03, 0x02);
445 for (i
= 0; i
< 100; i
++) {
446 usleep_range(10000, 11000);
451 mutex_unlock(&ci
->lock
);
455 static int slot_shutdown(struct dvb_ca_en50221
*ca
, int slot
)
457 struct cxd
*ci
= ca
->data
;
459 dev_dbg(&ci
->client
->dev
, "%s\n", __func__
);
461 read_data(ca
, slot
, ci
->rbuf
, 0);
462 mutex_lock(&ci
->lock
);
463 write_reg(ci
, 0x00, 0x21);
464 write_reg(ci
, 0x06, 0x1F);
467 write_regm(ci
, 0x09, 0x08, 0x08);
468 write_regm(ci
, 0x20, 0x80, 0x80); /* Reset CAM Mode */
469 write_regm(ci
, 0x06, 0x07, 0x07); /* Clear IO Mode */
473 mutex_unlock(&ci
->lock
);
477 static int slot_ts_enable(struct dvb_ca_en50221
*ca
, int slot
)
479 struct cxd
*ci
= ca
->data
;
481 mutex_lock(&ci
->lock
);
482 write_regm(ci
, 0x09, 0x00, 0x08);
485 mutex_unlock(&ci
->lock
);
489 static int campoll(struct cxd
*ci
)
493 read_reg(ci
, 0x04, &istat
);
496 write_reg(ci
, 0x05, istat
);
506 read_reg(ci
, 0x01, &slotstat
);
507 if (!(2 & slotstat
)) {
508 if (!ci
->slot_stat
) {
510 DVB_CA_EN50221_POLL_CAM_PRESENT
;
511 write_regm(ci
, 0x03, 0x08, 0x08);
517 write_regm(ci
, 0x03, 0x00, 0x08);
518 dev_info(&ci
->client
->dev
, "NO CAM\n");
523 ci
->slot_stat
== DVB_CA_EN50221_POLL_CAM_PRESENT
) {
525 ci
->slot_stat
|= DVB_CA_EN50221_POLL_CAM_READY
;
531 static int poll_slot_status(struct dvb_ca_en50221
*ca
, int slot
, int open
)
533 struct cxd
*ci
= ca
->data
;
536 mutex_lock(&ci
->lock
);
538 read_reg(ci
, 0x01, &slotstat
);
539 mutex_unlock(&ci
->lock
);
541 return ci
->slot_stat
;
544 static int read_data(struct dvb_ca_en50221
*ca
, int slot
, u8
*ebuf
, int ecount
)
546 struct cxd
*ci
= ca
->data
;
550 mutex_lock(&ci
->lock
);
552 mutex_unlock(&ci
->lock
);
557 mutex_lock(&ci
->lock
);
558 read_reg(ci
, 0x0f, &msb
);
559 read_reg(ci
, 0x10, &lsb
);
560 len
= ((u16
)msb
<< 8) | lsb
;
561 if (len
> ecount
|| len
< 2) {
562 /* read it anyway or cxd may hang */
563 read_block(ci
, 0x12, ci
->rbuf
, len
);
564 mutex_unlock(&ci
->lock
);
567 read_block(ci
, 0x12, ebuf
, len
);
569 mutex_unlock(&ci
->lock
);
573 static int write_data(struct dvb_ca_en50221
*ca
, int slot
, u8
*ebuf
, int ecount
)
575 struct cxd
*ci
= ca
->data
;
579 mutex_lock(&ci
->lock
);
580 write_reg(ci
, 0x0d, ecount
>> 8);
581 write_reg(ci
, 0x0e, ecount
& 0xff);
582 write_block(ci
, 0x11, ebuf
, ecount
);
584 mutex_unlock(&ci
->lock
);
588 static const struct dvb_ca_en50221 en_templ
= {
589 .read_attribute_mem
= read_attribute_mem
,
590 .write_attribute_mem
= write_attribute_mem
,
591 .read_cam_control
= read_cam_control
,
592 .write_cam_control
= write_cam_control
,
593 .slot_reset
= slot_reset
,
594 .slot_shutdown
= slot_shutdown
,
595 .slot_ts_enable
= slot_ts_enable
,
596 .poll_slot_status
= poll_slot_status
,
597 .read_data
= read_data
,
598 .write_data
= write_data
,
601 static int cxd2099_probe(struct i2c_client
*client
)
604 struct cxd2099_cfg
*cfg
= client
->dev
.platform_data
;
605 static const struct regmap_config rm_cfg
= {
612 ci
= kzalloc(sizeof(*ci
), GFP_KERNEL
);
619 memcpy(&ci
->cfg
, cfg
, sizeof(ci
->cfg
));
621 ci
->regmap
= regmap_init_i2c(client
, &rm_cfg
);
622 if (IS_ERR(ci
->regmap
)) {
623 ret
= PTR_ERR(ci
->regmap
);
627 ret
= regmap_read(ci
->regmap
, 0x00, &val
);
629 dev_info(&client
->dev
, "No CXD2099AR detected at 0x%02x\n",
634 mutex_init(&ci
->lock
);
635 ci
->lastaddress
= 0xff;
636 ci
->clk_reg_b
= 0x4a;
637 ci
->clk_reg_f
= 0x1b;
642 dev_info(&client
->dev
, "Attached CXD2099AR at 0x%02x\n", client
->addr
);
647 ci
->en
.read_data
= NULL
;
648 ci
->en
.write_data
= NULL
;
650 dev_info(&client
->dev
, "Using CXD2099AR buffer mode");
653 i2c_set_clientdata(client
, ci
);
658 regmap_exit(ci
->regmap
);
666 static void cxd2099_remove(struct i2c_client
*client
)
668 struct cxd
*ci
= i2c_get_clientdata(client
);
670 regmap_exit(ci
->regmap
);
674 static const struct i2c_device_id cxd2099_id
[] = {
678 MODULE_DEVICE_TABLE(i2c
, cxd2099_id
);
680 static struct i2c_driver cxd2099_driver
= {
684 .probe
= cxd2099_probe
,
685 .remove
= cxd2099_remove
,
686 .id_table
= cxd2099_id
,
689 module_i2c_driver(cxd2099_driver
);
691 MODULE_DESCRIPTION("Sony CXD2099AR Common Interface controller driver");
692 MODULE_AUTHOR("Ralph Metzler");
693 MODULE_LICENSE("GPL v2");