Merge tag 'v3.3.7' into 3.3/master
[zen-stable.git] / drivers / staging / media / cxd2099 / cxd2099.c
blob1c04185bcfd72d95bfe08a2b426fb9e1c1aaef6e
1 /*
2 * cxd2099.c: Driver for the CXD2099AR Common Interface Controller
4 * Copyright (C) 2010-2011 Digital Devices GmbH
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * 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.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 * 02110-1301, USA
22 * Or, point your browser to http://www.gnu.org/copyleft/gpl.html
25 #include <linux/slab.h>
26 #include <linux/kernel.h>
27 #include <linux/module.h>
28 #include <linux/moduleparam.h>
29 #include <linux/init.h>
30 #include <linux/i2c.h>
31 #include <linux/wait.h>
32 #include <linux/delay.h>
33 #include <linux/mutex.h>
34 #include <linux/io.h>
36 #include "cxd2099.h"
38 #define MAX_BUFFER_SIZE 248
40 struct cxd {
41 struct dvb_ca_en50221 en;
43 struct i2c_adapter *i2c;
44 struct cxd2099_cfg cfg;
46 u8 regs[0x23];
47 u8 lastaddress;
48 u8 clk_reg_f;
49 u8 clk_reg_b;
50 int mode;
51 int ready;
52 int dr;
53 int slot_stat;
55 u8 amem[1024];
56 int amem_read;
58 int cammode;
59 struct mutex lock;
62 static int i2c_write_reg(struct i2c_adapter *adapter, u8 adr,
63 u8 reg, u8 data)
65 u8 m[2] = {reg, data};
66 struct i2c_msg msg = {.addr = adr, .flags = 0, .buf = m, .len = 2};
68 if (i2c_transfer(adapter, &msg, 1) != 1) {
69 printk(KERN_ERR "Failed to write to I2C register %02x@%02x!\n",
70 reg, adr);
71 return -1;
73 return 0;
76 static int i2c_write(struct i2c_adapter *adapter, u8 adr,
77 u8 *data, u8 len)
79 struct i2c_msg msg = {.addr = adr, .flags = 0, .buf = data, .len = len};
81 if (i2c_transfer(adapter, &msg, 1) != 1) {
82 printk(KERN_ERR "Failed to write to I2C!\n");
83 return -1;
85 return 0;
88 static int i2c_read_reg(struct i2c_adapter *adapter, u8 adr,
89 u8 reg, u8 *val)
91 struct i2c_msg msgs[2] = {{.addr = adr, .flags = 0,
92 .buf = &reg, .len = 1},
93 {.addr = adr, .flags = I2C_M_RD,
94 .buf = val, .len = 1} };
96 if (i2c_transfer(adapter, msgs, 2) != 2) {
97 printk(KERN_ERR "error in i2c_read_reg\n");
98 return -1;
100 return 0;
103 static int i2c_read(struct i2c_adapter *adapter, u8 adr,
104 u8 reg, u8 *data, u8 n)
106 struct i2c_msg msgs[2] = {{.addr = adr, .flags = 0,
107 .buf = &reg, .len = 1},
108 {.addr = adr, .flags = I2C_M_RD,
109 .buf = data, .len = n} };
111 if (i2c_transfer(adapter, msgs, 2) != 2) {
112 printk(KERN_ERR "error in i2c_read\n");
113 return -1;
115 return 0;
118 static int read_block(struct cxd *ci, u8 adr, u8 *data, u8 n)
120 int status;
122 status = i2c_write_reg(ci->i2c, ci->cfg.adr, 0, adr);
123 if (!status) {
124 ci->lastaddress = adr;
125 status = i2c_read(ci->i2c, ci->cfg.adr, 1, data, n);
127 return status;
130 static int read_reg(struct cxd *ci, u8 reg, u8 *val)
132 return read_block(ci, reg, val, 1);
136 static int read_pccard(struct cxd *ci, u16 address, u8 *data, u8 n)
138 int status;
139 u8 addr[3] = {2, address & 0xff, address >> 8};
141 status = i2c_write(ci->i2c, ci->cfg.adr, addr, 3);
142 if (!status)
143 status = i2c_read(ci->i2c, ci->cfg.adr, 3, data, n);
144 return status;
147 static int write_pccard(struct cxd *ci, u16 address, u8 *data, u8 n)
149 int status;
150 u8 addr[3] = {2, address & 0xff, address >> 8};
152 status = i2c_write(ci->i2c, ci->cfg.adr, addr, 3);
153 if (!status) {
154 u8 buf[256] = {3};
155 memcpy(buf+1, data, n);
156 status = i2c_write(ci->i2c, ci->cfg.adr, buf, n+1);
158 return status;
161 static int read_io(struct cxd *ci, u16 address, u8 *val)
163 int status;
164 u8 addr[3] = {2, address & 0xff, address >> 8};
166 status = i2c_write(ci->i2c, ci->cfg.adr, addr, 3);
167 if (!status)
168 status = i2c_read(ci->i2c, ci->cfg.adr, 3, val, 1);
169 return status;
172 static int write_io(struct cxd *ci, u16 address, u8 val)
174 int status;
175 u8 addr[3] = {2, address & 0xff, address >> 8};
176 u8 buf[2] = {3, val};
178 status = i2c_write(ci->i2c, ci->cfg.adr, addr, 3);
179 if (!status)
180 status = i2c_write(ci->i2c, ci->cfg.adr, buf, 2);
181 return status;
184 #if 0
185 static int read_io_data(struct cxd *ci, u8 *data, u8 n)
187 int status;
188 u8 addr[3] = { 2, 0, 0 };
190 status = i2c_write(ci->i2c, ci->cfg.adr, addr, 3);
191 if (!status)
192 status = i2c_read(ci->i2c, ci->cfg.adr, 3, data, n);
193 return 0;
196 static int write_io_data(struct cxd *ci, u8 *data, u8 n)
198 int status;
199 u8 addr[3] = {2, 0, 0};
201 status = i2c_write(ci->i2c, ci->cfg.adr, addr, 3);
202 if (!status) {
203 u8 buf[256] = {3};
204 memcpy(buf+1, data, n);
205 status = i2c_write(ci->i2c, ci->cfg.adr, buf, n + 1);
207 return 0;
209 #endif
211 static int write_regm(struct cxd *ci, u8 reg, u8 val, u8 mask)
213 int status;
215 status = i2c_write_reg(ci->i2c, ci->cfg.adr, 0, reg);
216 if (!status && reg >= 6 && reg <= 8 && mask != 0xff)
217 status = i2c_read_reg(ci->i2c, ci->cfg.adr, 1, &ci->regs[reg]);
218 ci->regs[reg] = (ci->regs[reg] & (~mask)) | val;
219 if (!status) {
220 ci->lastaddress = reg;
221 status = i2c_write_reg(ci->i2c, ci->cfg.adr, 1, ci->regs[reg]);
223 if (reg == 0x20)
224 ci->regs[reg] &= 0x7f;
225 return status;
228 static int write_reg(struct cxd *ci, u8 reg, u8 val)
230 return write_regm(ci, reg, val, 0xff);
233 #ifdef BUFFER_MODE
234 static int write_block(struct cxd *ci, u8 adr, u8 *data, int n)
236 int status;
237 u8 buf[256] = {1};
239 status = i2c_write_reg(ci->i2c, ci->cfg.adr, 0, adr);
240 if (!status) {
241 ci->lastaddress = adr;
242 memcpy(buf + 1, data, n);
243 status = i2c_write(ci->i2c, ci->cfg.adr, buf, n + 1);
245 return status;
247 #endif
249 static void set_mode(struct cxd *ci, int mode)
251 if (mode == ci->mode)
252 return;
254 switch (mode) {
255 case 0x00: /* IO mem */
256 write_regm(ci, 0x06, 0x00, 0x07);
257 break;
258 case 0x01: /* ATT mem */
259 write_regm(ci, 0x06, 0x02, 0x07);
260 break;
261 default:
262 break;
264 ci->mode = mode;
267 static void cam_mode(struct cxd *ci, int mode)
269 if (mode == ci->cammode)
270 return;
272 switch (mode) {
273 case 0x00:
274 write_regm(ci, 0x20, 0x80, 0x80);
275 break;
276 case 0x01:
277 #ifdef BUFFER_MODE
278 if (!ci->en.read_data)
279 return;
280 printk(KERN_INFO "enable cam buffer mode\n");
281 /* write_reg(ci, 0x0d, 0x00); */
282 /* write_reg(ci, 0x0e, 0x01); */
283 write_regm(ci, 0x08, 0x40, 0x40);
284 /* read_reg(ci, 0x12, &dummy); */
285 write_regm(ci, 0x08, 0x80, 0x80);
286 #endif
287 break;
288 default:
289 break;
291 ci->cammode = mode;
296 static int init(struct cxd *ci)
298 int status;
300 mutex_lock(&ci->lock);
301 ci->mode = -1;
302 do {
303 status = write_reg(ci, 0x00, 0x00);
304 if (status < 0)
305 break;
306 status = write_reg(ci, 0x01, 0x00);
307 if (status < 0)
308 break;
309 status = write_reg(ci, 0x02, 0x10);
310 if (status < 0)
311 break;
312 status = write_reg(ci, 0x03, 0x00);
313 if (status < 0)
314 break;
315 status = write_reg(ci, 0x05, 0xFF);
316 if (status < 0)
317 break;
318 status = write_reg(ci, 0x06, 0x1F);
319 if (status < 0)
320 break;
321 status = write_reg(ci, 0x07, 0x1F);
322 if (status < 0)
323 break;
324 status = write_reg(ci, 0x08, 0x28);
325 if (status < 0)
326 break;
327 status = write_reg(ci, 0x14, 0x20);
328 if (status < 0)
329 break;
331 #if 0
332 status = write_reg(ci, 0x09, 0x4D); /* Input Mode C, BYPass Serial, TIVAL = low, MSB */
333 if (status < 0)
334 break;
335 #endif
336 status = write_reg(ci, 0x0A, 0xA7); /* TOSTRT = 8, Mode B (gated clock), falling Edge, Serial, POL=HIGH, MSB */
337 if (status < 0)
338 break;
340 status = write_reg(ci, 0x0B, 0x33);
341 if (status < 0)
342 break;
343 status = write_reg(ci, 0x0C, 0x33);
344 if (status < 0)
345 break;
347 status = write_regm(ci, 0x14, 0x00, 0x0F);
348 if (status < 0)
349 break;
350 status = write_reg(ci, 0x15, ci->clk_reg_b);
351 if (status < 0)
352 break;
353 status = write_regm(ci, 0x16, 0x00, 0x0F);
354 if (status < 0)
355 break;
356 status = write_reg(ci, 0x17, ci->clk_reg_f);
357 if (status < 0)
358 break;
360 if (ci->cfg.clock_mode) {
361 if (ci->cfg.polarity) {
362 status = write_reg(ci, 0x09, 0x6f);
363 if (status < 0)
364 break;
365 } else {
366 status = write_reg(ci, 0x09, 0x6d);
367 if (status < 0)
368 break;
370 status = write_reg(ci, 0x20, 0x68);
371 if (status < 0)
372 break;
373 status = write_reg(ci, 0x21, 0x00);
374 if (status < 0)
375 break;
376 status = write_reg(ci, 0x22, 0x02);
377 if (status < 0)
378 break;
379 } else {
380 if (ci->cfg.polarity) {
381 status = write_reg(ci, 0x09, 0x4f);
382 if (status < 0)
383 break;
384 } else {
385 status = write_reg(ci, 0x09, 0x4d);
386 if (status < 0)
387 break;
390 status = write_reg(ci, 0x20, 0x28);
391 if (status < 0)
392 break;
393 status = write_reg(ci, 0x21, 0x00);
394 if (status < 0)
395 break;
396 status = write_reg(ci, 0x22, 0x07);
397 if (status < 0)
398 break;
401 status = write_regm(ci, 0x20, 0x80, 0x80);
402 if (status < 0)
403 break;
404 status = write_regm(ci, 0x03, 0x02, 0x02);
405 if (status < 0)
406 break;
407 status = write_reg(ci, 0x01, 0x04);
408 if (status < 0)
409 break;
410 status = write_reg(ci, 0x00, 0x31);
411 if (status < 0)
412 break;
414 /* Put TS in bypass */
415 status = write_regm(ci, 0x09, 0x08, 0x08);
416 if (status < 0)
417 break;
418 ci->cammode = -1;
419 cam_mode(ci, 0);
420 } while (0);
421 mutex_unlock(&ci->lock);
423 return 0;
426 static int read_attribute_mem(struct dvb_ca_en50221 *ca,
427 int slot, int address)
429 struct cxd *ci = ca->data;
430 #if 0
431 if (ci->amem_read) {
432 if (address <= 0 || address > 1024)
433 return -EIO;
434 return ci->amem[address];
437 mutex_lock(&ci->lock);
438 write_regm(ci, 0x06, 0x00, 0x05);
439 read_pccard(ci, 0, &ci->amem[0], 128);
440 read_pccard(ci, 128, &ci->amem[0], 128);
441 read_pccard(ci, 256, &ci->amem[0], 128);
442 read_pccard(ci, 384, &ci->amem[0], 128);
443 write_regm(ci, 0x06, 0x05, 0x05);
444 mutex_unlock(&ci->lock);
445 return ci->amem[address];
446 #else
447 u8 val;
448 mutex_lock(&ci->lock);
449 set_mode(ci, 1);
450 read_pccard(ci, address, &val, 1);
451 mutex_unlock(&ci->lock);
452 /* printk(KERN_INFO "%02x:%02x\n", address,val); */
453 return val;
454 #endif
457 static int write_attribute_mem(struct dvb_ca_en50221 *ca, int slot,
458 int address, u8 value)
460 struct cxd *ci = ca->data;
462 mutex_lock(&ci->lock);
463 set_mode(ci, 1);
464 write_pccard(ci, address, &value, 1);
465 mutex_unlock(&ci->lock);
466 return 0;
469 static int read_cam_control(struct dvb_ca_en50221 *ca,
470 int slot, u8 address)
472 struct cxd *ci = ca->data;
473 u8 val;
475 mutex_lock(&ci->lock);
476 set_mode(ci, 0);
477 read_io(ci, address, &val);
478 mutex_unlock(&ci->lock);
479 return val;
482 static int write_cam_control(struct dvb_ca_en50221 *ca, int slot,
483 u8 address, u8 value)
485 struct cxd *ci = ca->data;
487 mutex_lock(&ci->lock);
488 set_mode(ci, 0);
489 write_io(ci, address, value);
490 mutex_unlock(&ci->lock);
491 return 0;
494 static int slot_reset(struct dvb_ca_en50221 *ca, int slot)
496 struct cxd *ci = ca->data;
498 mutex_lock(&ci->lock);
499 #if 0
500 write_reg(ci, 0x00, 0x21);
501 write_reg(ci, 0x06, 0x1F);
502 write_reg(ci, 0x00, 0x31);
503 #else
504 #if 0
505 write_reg(ci, 0x06, 0x1F);
506 write_reg(ci, 0x06, 0x2F);
507 #else
508 cam_mode(ci, 0);
509 write_reg(ci, 0x00, 0x21);
510 write_reg(ci, 0x06, 0x1F);
511 write_reg(ci, 0x00, 0x31);
512 write_regm(ci, 0x20, 0x80, 0x80);
513 write_reg(ci, 0x03, 0x02);
514 ci->ready = 0;
515 #endif
516 #endif
517 ci->mode = -1;
519 int i;
520 #if 0
521 u8 val;
522 #endif
523 for (i = 0; i < 100; i++) {
524 msleep(10);
525 #if 0
526 read_reg(ci, 0x06, &val);
527 printk(KERN_INFO "%d:%02x\n", i, val);
528 if (!(val&0x10))
529 break;
530 #else
531 if (ci->ready)
532 break;
533 #endif
536 mutex_unlock(&ci->lock);
537 /* msleep(500); */
538 return 0;
541 static int slot_shutdown(struct dvb_ca_en50221 *ca, int slot)
543 struct cxd *ci = ca->data;
545 printk(KERN_INFO "slot_shutdown\n");
546 mutex_lock(&ci->lock);
547 write_regm(ci, 0x09, 0x08, 0x08);
548 write_regm(ci, 0x20, 0x80, 0x80); /* Reset CAM Mode */
549 write_regm(ci, 0x06, 0x07, 0x07); /* Clear IO Mode */
550 ci->mode = -1;
551 mutex_unlock(&ci->lock);
552 return 0;
555 static int slot_ts_enable(struct dvb_ca_en50221 *ca, int slot)
557 struct cxd *ci = ca->data;
559 mutex_lock(&ci->lock);
560 write_regm(ci, 0x09, 0x00, 0x08);
561 set_mode(ci, 0);
562 #ifdef BUFFER_MODE
563 cam_mode(ci, 1);
564 #endif
565 mutex_unlock(&ci->lock);
566 return 0;
570 static int campoll(struct cxd *ci)
572 u8 istat;
574 read_reg(ci, 0x04, &istat);
575 if (!istat)
576 return 0;
577 write_reg(ci, 0x05, istat);
579 if (istat&0x40) {
580 ci->dr = 1;
581 printk(KERN_INFO "DR\n");
583 if (istat&0x20)
584 printk(KERN_INFO "WC\n");
586 if (istat&2) {
587 u8 slotstat;
589 read_reg(ci, 0x01, &slotstat);
590 if (!(2&slotstat)) {
591 if (!ci->slot_stat) {
592 ci->slot_stat |= DVB_CA_EN50221_POLL_CAM_PRESENT;
593 write_regm(ci, 0x03, 0x08, 0x08);
596 } else {
597 if (ci->slot_stat) {
598 ci->slot_stat = 0;
599 write_regm(ci, 0x03, 0x00, 0x08);
600 printk(KERN_INFO "NO CAM\n");
601 ci->ready = 0;
604 if (istat&8 && ci->slot_stat == DVB_CA_EN50221_POLL_CAM_PRESENT) {
605 ci->ready = 1;
606 ci->slot_stat |= DVB_CA_EN50221_POLL_CAM_READY;
609 return 0;
613 static int poll_slot_status(struct dvb_ca_en50221 *ca, int slot, int open)
615 struct cxd *ci = ca->data;
616 u8 slotstat;
618 mutex_lock(&ci->lock);
619 campoll(ci);
620 read_reg(ci, 0x01, &slotstat);
621 mutex_unlock(&ci->lock);
623 return ci->slot_stat;
626 #ifdef BUFFER_MODE
627 static int read_data(struct dvb_ca_en50221 *ca, int slot, u8 *ebuf, int ecount)
629 struct cxd *ci = ca->data;
630 u8 msb, lsb;
631 u16 len;
633 mutex_lock(&ci->lock);
634 campoll(ci);
635 mutex_unlock(&ci->lock);
637 printk(KERN_INFO "read_data\n");
638 if (!ci->dr)
639 return 0;
641 mutex_lock(&ci->lock);
642 read_reg(ci, 0x0f, &msb);
643 read_reg(ci, 0x10, &lsb);
644 len = (msb<<8)|lsb;
645 read_block(ci, 0x12, ebuf, len);
646 ci->dr = 0;
647 mutex_unlock(&ci->lock);
649 return len;
652 static int write_data(struct dvb_ca_en50221 *ca, int slot, u8 *ebuf, int ecount)
654 struct cxd *ci = ca->data;
656 mutex_lock(&ci->lock);
657 printk(kern_INFO "write_data %d\n", ecount);
658 write_reg(ci, 0x0d, ecount>>8);
659 write_reg(ci, 0x0e, ecount&0xff);
660 write_block(ci, 0x11, ebuf, ecount);
661 mutex_unlock(&ci->lock);
662 return ecount;
664 #endif
666 static struct dvb_ca_en50221 en_templ = {
667 .read_attribute_mem = read_attribute_mem,
668 .write_attribute_mem = write_attribute_mem,
669 .read_cam_control = read_cam_control,
670 .write_cam_control = write_cam_control,
671 .slot_reset = slot_reset,
672 .slot_shutdown = slot_shutdown,
673 .slot_ts_enable = slot_ts_enable,
674 .poll_slot_status = poll_slot_status,
675 #ifdef BUFFER_MODE
676 .read_data = read_data,
677 .write_data = write_data,
678 #endif
682 struct dvb_ca_en50221 *cxd2099_attach(struct cxd2099_cfg *cfg,
683 void *priv,
684 struct i2c_adapter *i2c)
686 struct cxd *ci = 0;
687 u8 val;
689 if (i2c_read_reg(i2c, cfg->adr, 0, &val) < 0) {
690 printk(KERN_INFO "No CXD2099 detected at %02x\n", cfg->adr);
691 return 0;
694 ci = kmalloc(sizeof(struct cxd), GFP_KERNEL);
695 if (!ci)
696 return 0;
697 memset(ci, 0, sizeof(*ci));
699 mutex_init(&ci->lock);
700 memcpy(&ci->cfg, cfg, sizeof(struct cxd2099_cfg));
701 ci->i2c = i2c;
702 ci->lastaddress = 0xff;
703 ci->clk_reg_b = 0x4a;
704 ci->clk_reg_f = 0x1b;
706 memcpy(&ci->en, &en_templ, sizeof(en_templ));
707 ci->en.data = ci;
708 init(ci);
709 printk(KERN_INFO "Attached CXD2099AR at %02x\n", ci->cfg.adr);
710 return &ci->en;
712 EXPORT_SYMBOL(cxd2099_attach);
714 MODULE_DESCRIPTION("cxd2099");
715 MODULE_AUTHOR("Ralph Metzler");
716 MODULE_LICENSE("GPL");